Scala: Nil vs List()

后端 未结 3 1255
隐瞒了意图╮
隐瞒了意图╮ 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:52

    As user unknown's answer shows, they are the same object.

    Idiomatically Nil should be preferred because it is nice and short. There's an exception though: if an explicit type is needed for whatever reason I think

    List[Foo]() 
    

    is nicer than

    Nil : List[Foo]
    

提交回复
热议问题