This page is no longer maintained — Please continue to the home page at

Re: bug or intended?

1 reply
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.

On Thu, Dec 29, 2011 at 2:11 PM, HamsterofDeath <h-star [at] gmx [dot] de> wrote:
so there is an intermediate array created which check if it is empty and
then returns Nil?

because List.apply is:

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

wouldn't it be better to return Nil directly?

scala> def f(s: String*) = s.getClass
f: (s: String*)java.lang.Class[_ <: String*]

scala> f()
res1: java.lang.Class[_ <: java.lang.String*] = class scala.collection.immutable.Nil$

Am 29.12.2011 14:00, schrieb Antoras:
> List() and Nil are identical:
> scala> List() == Nil
> res24: Boolean = true
> scala> List() eq Nil
> res25: Boolean = true
> Internally List() creates an empty collection.mutable.ListBuffer which
> returns Nil when toList is called on it.
> The "singleton"-word should not be there, I think. There is always a
> new list created if Some.toList is called. If None.toList is called,
> always Nil is returned which is a singleton. Probably it is a mistaken
> wording.
> Am 29.12.2011 12:02, schrieb HamsterofDeath:
>> option.toList
>>    /** Returns a singleton list containing the $option's value
>>     * if it is nonempty, or the empty list if the $option is empty.
>>     */
>>    def toList: List[A] =
>>      if (isEmpty) List() else List(this.get)
>> shouldn't it return Nil? did i find my first bug?
>> also, what is a "singleton list"? as far as i understand, a new list is
>> created every time i call toList

Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Simon Ochsenreither
Joined: 2011-07-17,
User offline. Last seen 42 years 45 weeks ago.
Re: bug or intended?
The signature changed recently (but not the semantics), so I wouldn't trust them too much. A few more method calls probably don't matter much, especially when the JITter does its job, but I would prefer Nil, too.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland