This page is no longer maintained — Please continue to the home page at www.scala-lang.org

bug or intended?

4 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

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

Julien Richard-Foy
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: bug or intended?

Nil is the same as List().A singleton list is a list containing only one element (here the one wrapped in the Option).

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: bug or intended?


On Thu, Dec 29, 2011 at 12:02 PM, HamsterofDeath <h-star [at] gmx [dot] de> wrote:
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

 scala> List() eq Nil
res0: Boolean = true


--
Viktor Klang

Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang
Antoras
Joined: 2010-05-23,
User offline. Last seen 1 year 19 weeks ago.
Re: bug or intended?

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
>
>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: bug or intended?

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?

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
>>
>>
>>
>

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