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

Why does Option not extend the Iterable trait?

5 replies
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
I asked this on StackOverflow but there have been no answers. Option in 2.8 even declares an iterator method but it does not implement Iterable, instead relying on an implicit conversion. Is this for reasons of conceptual purity, or is there some practical issue with it?

http://stackoverflow.com/questions/2607115/why-does-option-not-implement...

Chris

Get a free e-mail account with Hotmail. Sign-up now.
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Why does Option not extend the Iterable trait?

On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
> I asked this on StackOverflow but there have been no answers. Option
> in 2.8 even declares an iterator method but it does not implement
> Iterable, instead relying on an implicit conversion. Is this for
> reasons of conceptual purity, or is there some practical issue with
> it?

Option is not a sufficiently general container to fully interoperate
with the other collections.

scala> Seq(1) flatMap (_ => Iterable(1, 2))
res0: Seq[Int] = List(1, 2)

scala> Some(1) flatMap (_ => Iterable(1, 2))

Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Why does Option not extend the Iterable trait?

On 04/09/10 14:04, Paul Phillips wrote:
> On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
>> I asked this on StackOverflow but there have been no answers. Option
>> in 2.8 even declares an iterator method but it does not implement
>> Iterable, instead relying on an implicit conversion. Is this for
>> reasons of conceptual purity, or is there some practical issue with
>> it?
>
> Option is not a sufficiently general container to fully interoperate
> with the other collections.
>
> scala> Seq(1) flatMap (_ => Iterable(1, 2))
> res0: Seq[Int] = List(1, 2)
>
> scala> Some(1) flatMap (_ => Iterable(1, 2))
>
= res0 :) // same blocks passed
Seriously though,

Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
None : Option[T] flatMap ~> Seq[T] = List()

But as we've learned before with matz & POLS, the S is both subjective and
usually refers to the powers that be :-)

-Martin

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Why does Option not extend the Iterable trait?

On Fri, Apr 09, 2010 at 02:14:01PM -0400, Martin S. Weber wrote:
> Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
> None : Option[T] flatMap ~> Seq[T] = List()

Now Option has ceased to be a monad.

> But as we've learned before with matz & POLS, the S is both subjective
> and usually refers to the powers that be :-)

Yes, I would find it rather surprising if Option's monadic methods
starting returning Lists. POLS may be subjective but programming isn't
all about avoiding surprises: there are more important things, like
consistency of theoretical underpinnings.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Why does Option not extend the Iterable trait?
This in itself I don't see the problem with. For instance:   scala> Map(1 -> 2, 3 -> 4) flatMap ( _ => Map('a -> 'b))
res44: scala.collection.immutable.Map[Symbol,Symbol] = Map(('a,'b))   scala> Map(1 -> 2, 3 -> 4) flatMap ( _ => Iterable('a, 'b'))
res45: scala.collection.immutable.Iterable[Any] = List('a, b, 'a, b)

The problem I do see is that CanBuildFrom can't help here. It would require overload, but one can't overload based a passed function's type parameters. I take your point, though.
On Fri, Apr 9, 2010 at 3:04 PM, Paul Phillips <paulp@improving.org> wrote:
On Fri, Apr 09, 2010 at 04:00:06PM +0000, christopher marshall wrote:
> I asked this on StackOverflow but there have been no answers. Option
> in 2.8 even declares an iterator method but it does not implement
> Iterable, instead relying on an implicit conversion. Is this for
> reasons of conceptual purity, or is there some practical issue with
> it?

Option is not a sufficiently general container to fully interoperate
with the other collections.

scala> Seq(1) flatMap (_ => Iterable(1, 2))
res0: Seq[Int] = List(1, 2)

scala> Some(1) flatMap (_ => Iterable(1, 2))
<your answer here>

--
Paul Phillips      | Atheists dig the best foxholes.
In Theory          |
Empiricist         |
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------



--
Daniel C. Sobral

I travel to the future all the time.
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Why does Option not extend the Iterable trait?
Thanks - great answer. It seems to re-inforce Tony Morris' point about subclassing

> Date: Fri, 9 Apr 2010 11:18:32 -0700
> From: paulp@improving.org
> Subject: Re: [scala-user] Why does Option not extend the Iterable trait?
>
> On Fri, Apr 09, 2010 at 02:14:01PM -0400, Martin S. Weber wrote:
> > Some(x) : Option[T] flatMap ~> Seq[T] = List(x)
> > None : Option[T] flatMap ~> Seq[T] = List()
>
> Now Option has ceased to be a monad.


Get a free e-mail account with Hotmail. Sign-up now.

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