gpt4 book ai didi

scala - 传递LUB?

转载 作者:行者123 更新时间:2023-12-04 14:44:33 26 4
gpt4 key购买 nike

在它编译的这个片段中需要改变什么?

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元素是 HListInt元素。我们可以通过将连接的类型提取到类型变量中来帮助它( 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com