我试图在getAuthor方法中从book获取Option[Author]:
import shapeless.ops.record._
case class Book(title: String, author: Option[Author])
case class Author(name: String)
val book = Book("Types and Programming Languages", Option(Author("Benjamin Pierce")))
val w = Witness('author)
def getAuthor[T1, T2 <: HList, T3](t: T1)(implicit gen: LabelledGeneric.Aux[T1, T2], s: Selector.Aux[T2, w.T, Option[T3]]): s.Out = {
val repr = gen.to(t)
val opt = s.apply(repr)
opt.foreach {
??? // do something
}
opt
}
val author = getAuthor(book)
println(author)但是这段代码会导致以下编译错误:
could not find implicit value for parameter s: shapeless.ops.record.Selector.Aux[T2,shapeless.DuckTyping.w.T,Option[T3]] (No field shapeless.DuckTyping.w.T in record T2)
val author = getAuthor(book)我希望避免编译错误,并将Selector#apply返回值指定为Option[T]。
有人能帮帮忙吗?
发布于 2021-09-25 10:26:30
你似乎对暗示过度约束了。
尝试修改方法签名
def getAuthor[T1, T2 <: HList, T3](t: T1)(implicit
gen: LabelledGeneric.Aux[T1, T2],
s: Selector.Aux[T2, w.T, T3],
ev: T3 <:< Option[_]
): s.Out /* T3 */HList foldLeft with tuple as zero
Why is this implicit resolution failing?
Scala shapeless Generic.Aux implicit parameter not found in unapply
Extract FieldType key and value from HList
How to infer inner type of Shapeless record value with unary type constructor?
How to implicitly figure out the type at the head of a shapeless HList
https://stackoverflow.com/questions/69325140
复制相似问题