无形类型转换

Type transformation with shapeless

我有一个 class 类似于:

class MyClass[T <: HList] {
  val x: ???
}

我的问题是 x val 的类型。我想要的是将 T HList 的每个类型 U 替换为 Option[U] 的 HList。 IE。如果我指定:

new MyClass[Int :: String :: HNil]

我希望 x 有一个类型 Option[Int] :: Option[String] :: HNil

这可能吗?怎么做?

您需要一个 Mapped 实例来证明 Tx 的类型具有以下关系:

import shapeless._, ops.hlist.Mapped

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

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

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

有很多方法可以解决这个问题,但它们需要进行一些额外的更改。例如,您可以允许推断两种类型参数:

import shapeless._, ops.hlist.Comapped

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

然后:

new MyClass(Option(0) :: Option("") :: HNil)

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

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]
}

然后:

MyClass[Int :: String :: HNil](Option(0) :: Option("") :: HNil)

哪种方法更合适取决于您如何使用 class。