Scala: Nil vs List()

后端 未结 3 1259
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 15:31

In Scala, is there any difference at all between Nil and List()?

If not, which one is more idiomatic Scala style? Both for creating new emp

3条回答
  •  伪装坚强ぢ
    2020-12-07 15:48

    scala> println (Nil == List())
    true
    
    scala> println (Nil eq List())
    true
    
    scala> println (Nil equals List())
    true
    
    scala> System.identityHashCode(Nil)
    374527572
    
    scala> System.identityHashCode(List())
    374527572
    

    Nil is more idiomatic and can be preferred in most cases. Questions?

提交回复
热议问题