List.empty vs. List()vs. new List() [英] List.empty vs. List() vs. new List()

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

问题描述

List.empty List()之间的区别)

推荐答案

首先, new List()将不工作,因为 List 类是抽象的。其他两个选项在 中定义如下,列表对象

First of all, new List() won't work, since the List class is abstract. The other two options are defined as follows in the List object:

override def empty[A]: List[A] = Nil
override def apply[A](xs: A*): List[A] = xs.toList

也就是说,它们基本上是等同的,所以它主要是风格问题。我更喜欢使用 empty ,因为我发现它更清楚,并减少括号。

I.e., they're essentially equivalent, so it's mostly a matter of style. I prefer to use empty because I find it clearer, and it cuts down on parentheses.

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

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