- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
bug or intended?
Thu, 2011-12-29, 12:02
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
Thu, 2011-12-29, 14:11
#2
Re: bug or intended?
On Thu, Dec 29, 2011 at 12:02 PM, HamsterofDeath <h-star [at] gmx [dot] de> wrote:
option.toListscala> List() eq Nil
/** 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
res0: Boolean = true
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Thu, 2011-12-29, 14:21
#3
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
>
>
>
Thu, 2011-12-29, 14:31
#4
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
>>
>>
>>
>









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