首页 > 解决方案 > 如何提取缩小类型并将其用于 Shapeless 的副产品?

问题描述

有这个代码:

object Types {
  val undefined = "undefined".narrow
  type RunnableArgument = BigDecimal :+: String :+: Boolean :+: CNil
}

我想将undefined常量的类型作为RunnableArgumenthlist 元素之一。甚至可能吗?

标签: scalashapeless

解决方案


在 scala 2.13 中用作 HList 值的单例类型,请检查原始 SIP:

https://docs.scala-lang.org/sips/42.type.html

所以,在你的情况下,你可以写:

RunnableArgument = "undefined" :+: String :+: Boolean :+: CNil

你也可以使用这个东西作为类型:

type Param = "singleton type"

这在 scastie 中编译:

https://scastie.scala-lang.org/ObzN7xkKQ9egGBIv2gDuiA


推荐阅读