Scala 删除不成形的HList文本类型?
我试图按文本类型筛选HList,但无法使其正常工作,它只是筛选所有字符串:Scala 删除不成形的HList文本类型?,scala,shapeless,hlist,Scala,Shapeless,Hlist,我试图按文本类型筛选HList,但无法使其正常工作,它只是筛选所有字符串: import shapeless._ import shapeless.ops.hlist._ def filterLabel[Labels <: HList, Label, Out <: HList](labels: Labels, label: Label)( implicit sel: Filter.Aux[Labels, label.type, Out]): Out = sel(labels) va
import shapeless._
import shapeless.ops.hlist._
def filterLabel[Labels <: HList, Label, Out <: HList](labels: Labels, label: Label)(
implicit sel: Filter.Aux[Labels, label.type, Out]): Out = sel(labels)
val a: "a" = "a"
val b: "b" = "b"
val c1: 1 = 1
val h1 = a :: b :: a :: c1 :: HNil
//result: hl: String :: String :: String :: Int :: HNil = "a" :: "b" :: "a" :: 1 :: HNil
//looks like literal-types are no longer present?
filterLabel(h1, "a")
更新:
对于Lightbend Scala,我现在有以下内容:
import shapeless._
import shapeless.ops.hlist._
import syntax.singleton._
implicit def stringToF(label: String) = () => label.narrow
def filterLabel[Labels <: HList, T, Label <: String, Out <: HList](labels: Labels, label: () => Label)(
implicit
sel: Filter.Aux[Labels, Label, Out]): Out = sel(labels)
val h1 = "a".narrow :: "b".narrow :: "a".narrow :: "c".narrow :: HNil
filterLabel(h1, "a")
//res92: a :: a :: HNil = "a" :: "a" :: HNil
import shapeless.ops.hlist.Filter
import shapeless.{::, HList, HNil, Witness}
import shapeless.syntax.singleton._
def filterLabel[Labels <: HList, Label, Out <: HList](labels: Labels, label: Label)(
implicit sel: Filter.Aux[Labels, Label, Out]): Out = sel(labels)
val h1: Witness.`"a"`.T :: Witness.`"b"`.T :: Witness.`"a"`.T :: Witness.`1`.T :: HNil =
"a".narrow :: "b".narrow :: "a".narrow :: 1.narrow :: HNil
filterLabel(h1, "a".narrow) // a :: a :: HNil
导入无形状_
导入shapeless.ops.hlist_
导入syntax.singleton_
隐式def stringToF(label:String)=()=>label.窄
def filterLabel[标签我想这适用于Lightbend Scala:
import shapeless._
import shapeless.ops.hlist._
import syntax.singleton._
implicit def stringToF(label: String) = () => label.narrow
def filterLabel[Labels <: HList, T, Label <: String, Out <: HList](labels: Labels, label: () => Label)(
implicit
sel: Filter.Aux[Labels, Label, Out]): Out = sel(labels)
val h1 = "a".narrow :: "b".narrow :: "a".narrow :: "c".narrow :: HNil
filterLabel(h1, "a")
//res92: a :: a :: HNil = "a" :: "a" :: HNil
import shapeless.ops.hlist.Filter
import shapeless.{::, HList, HNil, Witness}
import shapeless.syntax.singleton._
def filterLabel[Labels <: HList, Label, Out <: HList](labels: Labels, label: Label)(
implicit sel: Filter.Aux[Labels, Label, Out]): Out = sel(labels)
val h1: Witness.`"a"`.T :: Witness.`"b"`.T :: Witness.`"a"`.T :: Witness.`1`.T :: HNil =
"a".narrow :: "b".narrow :: "a".narrow :: 1.narrow :: HNil
filterLabel(h1, "a".narrow) // a :: a :: HNil
导入shapeless.ops.hlist.Filter
导入无形状。{::,HList,HNil,Witness}
导入shapless.syntax.singleton_
def filterLabel[Labels这是我在类型级别scala 2.12.4上执行的方式
trait Narrow[T] {
def value: T
}
object Narrow {
def apply[T](implicit n: Narrow[T]): n.type = n
implicit def hnil = new Narrow[HNil] { def value = HNil }
implicit def cons[T, L <: HList: Narrow](implicit t: ValueOf[T]) = new Narrow[T::L] {
def value = t.value :: Narrow[L].value
}
}
我怀疑这个问题与不成形有关。我猜如果你选中:键入a
,你会得到字符串
。即使使用stock编译器,尤其是在REPL和2.12上,这个东西也有惊人的缺陷。你可以尝试使用val a:Witness。
a“.T=”a“
或类型别名—我知道可以通过多种方法让它记住singleton类型,至少在vanilla 2.12中是这样。
scala> val h1 = Narrow["a"::"b"::"a"::1::HNil].value
h1: "a" :: "b" :: "a" :: 1 :: shapeless.HNil = a :: b :: a :: 1 :: HNil