- About Scala
- In the Enterprise
- Scala Community
- Language Research
- In the Press
- The Scala Team
- Scala's Prehistory
- Contact Us
- Learning Scala
- Tour of Scala
- Scala API
- Setup & Getting Started
- Programming Guides
- Other Guides
- Code Examples
- Scala Developers
Re: bug or intended?
Thu, 2011-12-29, 14:18
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*]
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
> Am 29.12.2011 12:02, schrieb HamsterofDeath:
>> /** 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
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts