首页 文章

Scala默认值:如何在创建LinkedList时从Scala中的Any类型中获取默认值?

提问于
浏览
2

我在Scala中创建了一个Doublelylinked列表,我在创建Sentinel节点时遇到了问题 . 对于我的sentinel节点的数据,我需要我正在使用的类型的默认值(EX:我需要0表示Int或null表示String) . 只使用A不会得到这个值,Scala Manifest不会给我这个 Value . 有关如何在Eclipse中的这种情况下获取Scala中的Any的默认值的任何想法?

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
}

1 回答

  • 0

    这工作(至少在控制台,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
    

相关问题