如何正确键入 - 注释此HList?

时间:2010-10-11 15:41:05

标签: scala scala-2.8 hlist

sealed abstract trait HList

case class :+:[H, T <: HList](head: H, tail: T) extends HList {
  def :+:[T](v: T) = new :+:(v, this)
}

case object HNil extends HList {
  def :+:[T](v: T) = new :+:(v, this)
}

object HListExpt {
  def main(args: Array[String]) {
    val me: String :+: Int :+: Symbol :+: HNil.type = "Rahul" :+: 20 :+: 'Male :+: HNil
    println(me.head, me.tail.head)
  }
}

在尝试编译上面的代码时,我得到以下编译器错误:

error: type mismatch;
found   : :+:[java.lang.String,:+:[Int,:+:[Symbol,object HNil]]]
required: :+:[String,:+:[Int,:+:[Symbol,HNil.type]]]
val me: String :+: Int :+: Symbol :+: HNil.type = "Rahul" :+: 20 :+: 'Male :+: HNil

我在这里做错了什么?键入以上HList

的正确方法是什么?

PS:当我删除类型注释时,代码编译得很好。

2 个答案:

答案 0 :(得分:7)

这里的根本问题是从不推断单例类型。这是一个演示:

scala> case object A      
defined module A

scala> A                  
res6: A.type = A

scala> identity[A.type](A)
res7: A.type = A

scala> identity(A)        
res8: object A = A

这是为什么? Quoth Odersky et。人。在Scala编程中,§27.6:

  

通常[单身]类型也是如此   具体是有用的,这就是原因   编译器不愿意插入   他们是自动的。

所以,让我们明确提供类型参数:

sealed abstract trait HList

case class :+:[H, T <: HList](head: H, tail: T) extends HList {
  def :+:[T](v: T) = new :+:(v, this)
}

case object HNil extends HList {
  def :+:[T](v: T) = new :+:[T, HNil.type](v, this)
}

val me: String :+: Int :+: Symbol :+: HNil.type = "Rahul" :+: 20 :+: 'Male :+: HNil
println(me.head, me.tail.head)

奖金链接:

答案 1 :(得分:2)

我不知道为什么,但是如果将HNil定义为类,则所有内容都会编译:

class HNilClass extends HList {
  def :+:[T](v: T) = new :+:(v, this)
}

object HNil extends HNilClass