differently-typed values in Map?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

differently-typed values in Map?

brian-245
Is there a way to have differently-typed values in a Map while being
able to get them and know their types for certain? Josh Bloch called
this idea a typesafe heterogeneous container.
Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

Ingo Maier-3
brian wrote:
> Is there a way to have differently-typed values in a Map while being
> able to get them and know their types for certain? Josh Bloch called
> this idea a typesafe heterogeneous container.
>

Are you looking for something like this:
http://thread.gmane.org/gmane.comp.lang.scala/12629

Ingo
Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

Ricky Clarkson
val stringsAndInts=List(Left("hello"), Right(5), Right(10), Left("goodbye"))

stringsAndInts flatMap (_.right.toSeq) reduceLeft (_+_) gives 15 (5 + 10)

I have no idea why _.right doesn't return an Iterable.

2008/7/25 Ingo Maier <[hidden email]>:
brian wrote:
Is there a way to have differently-typed values in a Map while being
able to get them and know their types for certain? Josh Bloch called
this idea a typesafe heterogeneous container.


Are you looking for something like this:
http://thread.gmane.org/gmane.comp.lang.scala/12629

Ingo

Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

Alex Boisvert-3
On Fri, Jul 25, 2008 at 1:19 AM, Ricky Clarkson <[hidden email]> wrote:
val stringsAndInts=List(Left("hello"), Right(5), Right(10), Left("goodbye"))

stringsAndInts flatMap (_.right.toSeq) reduceLeft (_+_) gives 15 (5 + 10)

I have no idea why _.right doesn't return an Iterable.

Indeed, looks like Either.{Left|Right}Projection don't extend Iterable...   Tony? :)

alex


Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

David MacIver
On Fri, Jul 25, 2008 at 4:00 PM, Alex Boisvert <[hidden email]> wrote:

> On Fri, Jul 25, 2008 at 1:19 AM, Ricky Clarkson <[hidden email]>
> wrote:
>>
>> val stringsAndInts=List(Left("hello"), Right(5), Right(10),
>> Left("goodbye"))
>>
>> stringsAndInts flatMap (_.right.toSeq) reduceLeft (_+_) gives 15 (5 + 10)
>>
>> I have no idea why _.right doesn't return an Iterable.
>
> Indeed, looks like Either.{Left|Right}Projection don't extend Iterable...
> Tony? :)

There's actually a reason for this: They both offer flatMap methods
which are incompatible with Iterable's. (Same reason Option doesn't).

An implicit conversion to Iterable defined on their companion object
would be a good idea though.
Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

David MacIver
On Fri, Jul 25, 2008 at 4:01 PM, David MacIver <[hidden email]> wrote:

> On Fri, Jul 25, 2008 at 4:00 PM, Alex Boisvert <[hidden email]> wrote:
>> On Fri, Jul 25, 2008 at 1:19 AM, Ricky Clarkson <[hidden email]>
>> wrote:
>>>
>>> val stringsAndInts=List(Left("hello"), Right(5), Right(10),
>>> Left("goodbye"))
>>>
>>> stringsAndInts flatMap (_.right.toSeq) reduceLeft (_+_) gives 15 (5 + 10)
>>>
>>> I have no idea why _.right doesn't return an Iterable.
>>
>> Indeed, looks like Either.{Left|Right}Projection don't extend Iterable...
>> Tony? :)
>
> There's actually a reason for this: They both offer flatMap methods
> which are incompatible with Iterable's. (Same reason Option doesn't).

Well, let's qualify that: There's a reason this is a good idea. Tony
has said that he didn't even consider the possibility that they should
extend Iterable.
Reply | Threaded
Open this post in threaded view
|

Re: differently-typed values in Map?

Landei
In reply to this post by brian-245
brian-245 wrote
Is there a way to have differently-typed values in a Map while being
able to get them and know their types for certain? Josh Bloch called
this idea a typesafe heterogeneous container.
Do you mean something like here http://www.nabble.com/-scala--Real-dependent-typing-in-Scala!-td18357782.html ?