I am creating a Doublelylinked list in Scala and I have having a problem creating the sentinel node. For my sentinel node's data, I need the default value of the type I am using (EX: I need 0 for Int or null for String). Just using A does not get this value, Scala Manifest does not give me the value. Any ideas on how to get the default value for Any in Scala at this situation in Eclipse?

class DoubleLinkList[A : Manifest] extends List[A] {
private class Node (var data: A, var previous:Node, var next:Node)
private var end:Node new Node(A, null, null) //I need the default value of the type I am       using for A in this situation.
end.prev = end
end.next = end
}
有帮助吗?

解决方案

That works (in console at least, scala 2.10.3):

scala> def defval[A:Manifest]() = null.asInstanceOf[A]
defval: [A]()(implicit evidence$1: Manifest[A])A

scala> defval[Boolean]
res26: Boolean = false
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top