类型转换与无形

Mar*_*mou 6 scala shapeless

我有一个类似这样的类:

class MyClass[T <: HList] {
  val x: ???
}
Run Code Online (Sandbox Code Playgroud)

我的问题是xval 的类型.我想要的是让它成为一个HList,每个类型UTHList被替换为Option[U].即如果我指定:

new MyClass[Int :: String :: HNil]
Run Code Online (Sandbox Code Playgroud)

我想x有一种类型Option[Int] :: Option[String] :: HNil

这甚至可能吗?怎么做?

Tra*_*own 9

你需要一个Mapped见证这种情况的实例Tx有这种关系的类型:

import shapeless._, ops.hlist.Mapped

abstract class MyClass[T <: HList, OT <: HList](implicit
  mapped: Mapped.Aux[T, Option, OT]
) {
  val x: OT
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,实例化这有点不方便:

new MyClass[Int :: String :: HNil, Option[Int] :: Option[String] :: HNil] {
  val x = Some(0) :: Some("") :: HNil
}
Run Code Online (Sandbox Code Playgroud)

有很多方法,但它们需要一些额外的更改.例如,您可以允许推断两个类型参数:

import shapeless._, ops.hlist.Comapped

class MyClass[T <: HList, OT <: HList](val x: OT)(implicit
  mapped: Comapped.Aux[OT, Option, T]
)
Run Code Online (Sandbox Code Playgroud)

然后:

new MyClass(Option(0) :: Option("") :: HNil)
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用随附对象中的自定义构造函数来使用更接近原始类的内容:

import shapeless._, ops.hlist.Mapped

abstract class MyClass[T <: HList] {
  type OT <: HList
  def mapped: Mapped.Aux[T, Option, OT]
  val x: OT
}

object MyClass {
  class PartiallyApplied[T <: HList] {
    def apply[OT0 <: HList](x0: OT0)(implicit
      mapped0: Mapped.Aux[T, Option, OT0]
    ): MyClass[T] =
      new MyClass[T] {
        type OT = OT0
        val mapped: Mapped.Aux[T, Option, OT] = mapped0
        val x: OT = x0
      }
  }

  def apply[T <: HList]: PartiallyApplied[T] = new PartiallyApplied[T]
}
Run Code Online (Sandbox Code Playgroud)

然后:

MyClass[Int :: String :: HNil](Option(0) :: Option("") :: HNil)
Run Code Online (Sandbox Code Playgroud)

哪种方法更合适取决于您如何使用该课程.