将一个Shapeless HList的类型映射出来

7

我一直在尝试映射 scala 的 shapeless 包中 HList 的类型,但没有访问其值的权限。

以下内容成功地映射了 HList 的值:

import shapeless._
import shapeless.Poly._
import ops.hlist.Mapper
import ops.hlist.Mapper._

trait Person {
  type Value
  val v : Value
}

case class StringPerson extends Person {
  type Value = String
  val v = "I like strings"
}

case class IntPerson extends Person {
  type Value = Int 
  val v = 42
}

object what_is_going_on {

  object test_value_op {
    val stringPerson = StringPerson()
    val intPerson = IntPerson()

    trait lpvfun extends Poly1 {
      implicit def default[A <: Person] = at[A](_.v)
    } 

    object vfun extends lpvfun {}

    // Use these to generate compiler errors if the mapped type is not what we'd expect:

    type TestListType = StringPerson :: IntPerson :: HNil
    type TestListExpectedMappedType = String :: Int :: HNil

    // Input:
    val testList : TestListType = stringPerson :: intPerson :: HNil

    // Output:
    val mappedList : TestListExpectedMappedType = testList map vfun

    // Get the actual mapped type 
    type TestListActualMappedType = mappedList.type

    // This compiles......
    val mappedList1 : TestListActualMappedType = mappedList

    // .... but weirdly this line doesn't. That isn't the point of this question, but I'd be very grateful for an answer.
    //implicitly[TestListActualMappedType =:= TestListExpectedMappedType]
  }

}

很棒!除了因某些原因无法使用implicitly[A =:= B]之外,一个HList的值已经被映射,它们的类型也一样。
现在,如果我们没有HList的值,但我们知道它的类型,如何映射它的类型?
根据这里map的定义(链接),我尝试了以下方法:
object test_type_op { 
  type TestListType = StringPerson :: IntPerson :: HNil
  type TestListExpectedMappedType = String :: Int :: HNil

  // Attempt 1 does not work, compiler cannot prove =:=
  type MappedType = Mapper[vfun.type, TestListType]#Out
  implicitly[MappedType =:= TestListExpectedMappedType]

  // Attempt 2 does not work, compiler cannot prove =:=
  class GetMapper {
    implicit val mapper : Mapper[vfun.type, TestListType]
    implicitly[mapper.Out =:= TestListExpectedMappedType]
  }

}

如果没有访问其值的权限,如何获取映射HList的类型?是否有一种调试方法可以查明编译器无法证明某些内容的原因?感谢您的阅读。

1个回答

4
TestListActualMappedType 中,您得到了与 mappedList 推断类型不同的单例类型。即使不涉及 Shapeless,您也可以看到完全相同的问题。
scala> val x = "foo"
x: String = foo

scala> implicitly[x.type =:= String]
<console>:13: error: Cannot prove that x.type =:= String.
       implicitly[x.type =:= String]
                 ^

您可以要求证明 x.typeString 的子类型,或者您可以使用 shapeless.test.typed,在您的情况下,它看起来像这样:
import shapeless._, ops.hlist.Mapper

trait Person {
  type Value
  val v : Value
}

case class StringPerson() extends Person {
  type Value = String
  val v = "I like strings"
}

case class IntPerson() extends Person {
  type Value = Int 
  val v = 42
}

trait lpvfun extends Poly1 {
  implicit def default[A <: Person] = at[A](_.v)
} 

object vfun extends lpvfun {}

val stringPerson = StringPerson()
val intPerson = IntPerson()

val testList = stringPerson :: intPerson :: HNil
val mappedList = testList map vfun

shapeless.test.typed[String :: Int :: HNil](mappedList)

虽然如此,这并不能为您带来太多好处,因为您仍需要明确指定类型。

您可以要求提供证据,以确保像Mapper这样的类型类的输出类型与特定输入类型所期望的类型相同:

scala> val m = Mapper[vfun.type, StringPerson :: IntPerson :: HNil]
m: shapeless.ops.hlist.Mapper[vfun.type,shapeless.::[StringPerson,shapeless.::[IntPerson,shapeless.HNil]]]{type Out = shapeless.::[String,shapeless.::[Int,shapeless.HNil]]} = shapeless.ops.hlist$Mapper$$anon$5@6f3598cd

scala> implicitly[m.Out =:= (String :: Int :: HNil)]
res1: =:=[m.Out,shapeless.::[String,shapeless.::[Int,shapeless.HNil]]] = <function1>

这更有可能是有用的,但再次取决于您试图说服自己的具体内容。


谢谢 - 这真的很有帮助。在您的第二个代码块中,m 在编译时会发生什么?它会被优化掉吗?有没有任何方法可以获取 m.Out 的类型而不实际创建任何值? - user1158559

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接