作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在它编译的这个片段中需要改变什么?
import shapeless._
import LUBConstraint._
import ops.hlist.Prepend
class Foo[L <: HList: <<:[Int]#λ](val l: L) {
def ++[H <: HList: <<:[Int]#λ](h: H)(implicit prepend: Prepend[L, H]) = {
new Foo(l ::: h)
}
}
could not find implicit value for evidence parameter of type shapeless.LUBConstraint[prepend.Out,Int]
最佳答案
好问题......你非常接近:-)
问题是,即使对我们来说很明显,Scala 编译器也无法推断出两个 HLists
的连接。的 Int
元素是 HList
的 Int
元素。我们可以通过将连接的类型提取到类型变量中来帮助它( Out
,注意使用 Prepend.Aux
类型别名来约束这个新类型变量)然后要求它直接证明 Out
具有所需的属性(通过要求证明 Out
的形状),
import shapeless._
import ops.hlist.Prepend
import LUBConstraint._
class Foo[L <: HList: <<:[Int]#λ](val l: L) {
def ++[H <: HList: <<:[Int]#λ, Out <: HList](h: H)
(implicit prepend: Prepend.Aux[L, H, Out], ev: LUBConstraint[Out, Int]) = {
new Foo(l ::: h)
}
}
关于scala - 传递LUB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462011/
给定一个具有协变类型参数的类: scala> class G[+A] defined class G 以下列表显示了 List[G[Any]] 的最小上限。 scala> List(new G[Int
Table 15.25-B在 JLS 版本 8 中说条件表达式的类型 true ? null : 0.0 是 lub(null,Double),其中 lub 似乎是来自 section 4.10.4
以下引自 JLS 14.20 The declared type of an exception parameter that denotes its type as aunion with alte
我想创建和使用 Seq[T] Scala 中的集合 并确保它只使用一种类型。所以如果我使用: val l = List(1, 2, 2.0) 应该发生编译时错误 - List元素都应该是Double或
我是一名优秀的程序员,十分优秀!