Scala:Nil 与 List() [英] Scala: Nil vs List()

查看:51
本文介绍了Scala:Nil 与 List()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Scala 中,NilList() 之间有什么区别吗?

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

如果不是,哪一种更符合 Scala 风格?用于创建新的空列表和空列表的模式匹配.

If not, which one is more idiomatic Scala style? Both for creating new empty lists and pattern matching on empty lists.

推荐答案

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 更惯用,在大多数情况下可以首选.有问题吗?

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

这篇关于Scala:Nil 与 List()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆