Scala Default Values: How do I get a default value from type Any in Scala while creating a LinkedList?

StackOverflow https://stackoverflow.com/questions/22101490

Domanda

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
}
È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top