加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 无形的类型转换

发布时间:2020-12-16 18:58:05 所属栏目:安全 来源:网络整理
导读:我有一个类似这样的类: class MyClass[T : HList] { val x: ???} 我的问题是x val的类型.我想要的是让它成为一个HList,T HList的每个类型U都被Option [U]取代.即如果我指定: new MyClass[Int :: String :: HNil] 我希望x有一个Option [Int] :: Option [Str
我有一个类似这样的类:

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

我的问题是x val的类型.我想要的是让它成为一个HList,T HList的每个类型U都被Option [U]取代.即如果我指定:

new MyClass[Int :: String :: HNil]

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

这甚至可能吗?怎么做?

解决方法

你需要一个Mapped实例来证明T和x的类型有这种关系:

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,T]
)

然后:

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

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

import shapeless._,ops.hlist.Mapped

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

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

  def apply[T <: HList]: PartiallyApplied[T] = new PartiallyApplied[T]
}

然后:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读