问题标签 [scala-generics]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
253 浏览

scala - 从 cassandra 表中读取的 scala-cass 泛型作为案例类

我正在尝试使用scala-cass来读取 cassandra 并将结果集转换为使用resultSet.as[CaseClass]. 这在运行以下命令时效果很好。

现在我试图使它更通用,但我无法为泛型类找到合适的类型约束。

我用所需的案例类实现这个类,并尝试在创建的对象上调用 getRows。

这会引发异常could not find implicit value for parameter ccd: com.weather.scalacass.CCCassFormatDecoder[T]

我正在尝试向 GenericReader 添加类型约束,以确保隐式转换能够正常工作。但是,我找不到合适的类型。我正在尝试通读 scala-cass 以找到适当的约束,但到目前为止我还没有运气。

我也很乐意使用任何其他可以实现这一目标的库。

0 投票
0 回答
33 浏览

scala - Reflectively determining compatibility of Scala generics

Given a target type (say List[String]) and some object o, the goal is to find a method of o with a return type that is compatible with the target type.

In the absence of generics, one can check this by comparing the target type and the return type of the method using the <:< operator (the scala reflection analog of Java's isAssignableFrom) from scala.reflect.runtime.universe.

This approach does not work in the presence of generics: for example, the return type of the method def getEmptyList[T]: List[T] = Nil does not satisfy List[T] <:< List[String]. How does one determine that the return type of getEmptyList[T] is indeed compatible with List[String]?

0 投票
1 回答
2118 浏览

scala - 自类型不符合基类

使用以下代码:

我在BasicHello案例类的 IDE 中收到以下错误:

我不知道为什么编译器不允许这样做,因为BasicHelloextends Hello[BasicBaz],而后者又是 extends Baz[BasicFoo, BasicBar]。的类型BasicHello应该Hello[Baz[Foo, Bar]]正是World混入所需要的。是否有一些我缺少自我类型的分层属性?

0 投票
2 回答
442 浏览

scala - 在Scala中扩展通用特征的返回类

斯卡拉代码:

0 投票
1 回答
318 浏览

scala - 协变类型出现在 HList 中的不变位置

我正在尝试定义一个类型安全的异构列表,它对其元素的类型有限制,在元素之间强制执行层次结构(例如,类型 A 不能出现在类型 B 之后)。尝试将我的结构转换为无形的 HList 时会出现问题。

以下是我如何为我的类型定义特征:

我收到以下错误:

这很令人费解,因为定义shapeless.::两个类型参数都是协变的。

我正在使用 scala 2.11.11 和 shapeless 2.3.2。有没有办法解决这个错误?

0 投票
3 回答
182 浏览

scala - 协变类型 FParam 出现在值猜测的类型 Seq[FParam] 的逆变位置

考虑这个简单的例子:

它不会编译,因为

协变类型FParam出现在Seq[FParam]值猜测类型中的逆变位置。

但是 seq 被定义为trait Seq[+A],那么这个逆变的来源是什么?(问题一

相反,考虑这个简单的例子-FParam

逆变类型出现在类型中的协变位置(FParam) => FRes

同样的悖论:在 中Function1[-T1, R],第一个类型参数显然是逆变的,那么为什么FParam处于协变位置呢?(问题2

我可以通过翻转Lower type bounds中描述的方差来解决这个问题,但是为什么它是必要的还不清楚。

0 投票
4 回答
766 浏览

scala - Scala 通用通配符和特定类型

mappings有价值:

是否有可能使它更类型安全

whereT与下划线的作用相同。如果它符合以下代码,我希望编译器会抱怨:

0 投票
1 回答
1064 浏览

scala - Scala类型推断错误

我编写了一个组合的 map-and-find 函数,它将函数应用于 Iterable 并返回谓词为 true 的第一个映射结果:

问题是当我尝试按预期使用该函数时遇到编译错误:

类型不匹配,预期:(NotInferedB) => Boolean,实际:(Nothing) => Any

如果我使用类型提示,尽管事情编译得很好:

为什么 typeB不能推断为Intfrom f

0 投票
1 回答
338 浏览

scala - Scala Trait type mismatch

I have some Kafka Channel hierarchy that I am using in my project:

My base trait is:

Now I have a common kafka send Channel like

Now there are 2 variants of CommanKafkaSendChannel, one is with callback and one is with Future:

KafkaSendChannelWithCallback definition:

Now based on the configuration value I select the proper type of channel on run time like below. I am creating actor with right type of channel which will send the data to kafka:

Actor definition:

The problem is when I use KafkaSendChannelWithCallback my code compiles properly however when I use KafkaSendChannelWithFuture it gives me below error on actor = declaration:

[error]IngestionActor.scala:32: pattern type is incompatible with expected type; [error] found : KafkaSendChannelWithFuture[String,V] [error] required: SendChannel[V,Unit]

As both the channel definitions are extended from SendChannel, this code should have compiled without any error. I am not sure why it is not compiling. Thanks

0 投票
1 回答
61 浏览

scala - 使用 Akka 演员获得正确的方差?

在使用泛型和 Akka Actor 时,我经常遇到以下问题:

AuctionParticipantActor只是一个不可变的包装器AuctionParticipant。我需要类型P是协变的,我不确定实现这一目标的最佳方法是什么。

或者,对于我的用例,我认为我什至不需要参数化AuctionParticipantActor. 我可以有类似的东西:

但在这种情况下,我不知道用什么代替???为了尊重类型绑定。如果有人认为我的问题出在设计上,请说出来。想法?