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

f(Some(3):_*) //not work :-)

5 replies
Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.

option doesn't work with :_*

def f (x:Int*) = { x.foldLeft(0)(_+_) }
//working
f(Some(3).toList:_*)
//not working
f(Some(3):_*)

​Shouldn't it work? Shouldn't :_* turn into some form of
for/map/flatMap/something regardless of the type?

play with it here: http://bit.ly/aGY0kF

-----
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toy: http://scripster.codewitter.com
Follow me: http://feeds.razie.com/RazvanTech RSS Feed ,
http://twitter.com/razie Twitter , http://github.com/razie GitHub .

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: f(Some(3):_*) //not work :-)


On Thu, Apr 15, 2010 at 9:33 AM, Razvan Cojocaru <razie [at] razie [dot] com> wrote:

option doesn't work with :_*

def f (x:Int*) = { x.foldLeft(0)(_+_) }
//working
f(Some(3).toList:_*)
//not working
f(Some(3):_*)

Shouldn't it work? Shouldn't :_* turn into some form of
for/map/flatMap/something regardless of the type?

Nope.  :_* means "I've got a Seq[T] (or something that has an implicit conversion to Seq[T]), so put the Seq into the varargs)"  I think there used to be an Option -> Iterable implicit conversion, but that may be gone.
 

play with it here: http://bit.ly/aGY0kF


-----
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toy: http://scripster.codewitter.com
Follow me:  http://feeds.razie.com/RazvanTech RSS Feed ,
http://twitter.com/razie Twitter ,  http://github.com/razie GitHub .

--
View this message in context: http://old.nabble.com/f%28Some%283%29%3A_*%29----not-work-%3A-%29-tp28257603p28257603.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: f(Some(3):_*) //not work :-)

On Thu, Apr 15, 2010 at 09:33:28AM -0700, Razvan Cojocaru wrote:
> ​Shouldn't it work? Shouldn't :_* turn into some form of
> for/map/flatMap/something regardless of the type?

:_* has nothing to do with for/map/flatMap things. It means "this is a
Seq, treat it like multiple arguments." As is covered periodically
around here, Option is not a Seq. There is an implicit from Option to
Iterable, but that's still not a Seq.

Of course Array isn't a Seq either, but I'm pretty sure this is supposed
to work...

scala> "%d %d".format(5, 10)
res4: String = 5 10

scala> "%d %d".format(Seq(5, 10): _*)
res5: String = 5 10

scala> "%d %d".format(Array(5, 10): _*)
java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;

But this works:

scala> "%d %d".format(Array[Integer](5, 10): _*)
warning: there were deprecation warnings; re-run with -deprecation for details
res7: String = 5 10

As does:

scala> "%d %d %d".format((Array(1,2,3): Seq[Int]) : _*)
res8: String = 1 2 3

I'm not sure what exactly is expected to work. But you shouldn't be
surprised Option doesn't.

Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.
Re: f(Some(3):_*) //not work :-)

Limiting it to just Seq may serve some internal implementation/technology
limitations but it doesn't fit my mental model anymore...I now read it to
mean something like "flatten a collection of things into a sequence of
arguments" ... True, since arguments have a natural order, the initial
collection better be ordered - I guess that's why you chose Seq :) but
Option is naturally just as...well, ordered :)

I guess my point was...since Option has a toList and Some(3).toList:_* works
(obviously), why wouldn't :_* work directly on Option...or anything that has
a toList for that matter?

Paul Phillips-3 wrote:
>
> On Thu, Apr 15, 2010 at 09:33:28AM -0700, Razvan Cojocaru wrote:
>> ​Shouldn't it work? Shouldn't :_* turn into some form of
>> for/map/flatMap/something regardless of the type?
>
> :_* has nothing to do with for/map/flatMap things. It means "this is a
> Seq, treat it like multiple arguments." As is covered periodically
> around here, Option is not a Seq. There is an implicit from Option to
> Iterable, but that's still not a Seq.
>
> Of course Array isn't a Seq either, but I'm pretty sure this is supposed
> to work...
>
> scala> "%d %d".format(5, 10)
> res4: String = 5 10
>
> scala> "%d %d".format(Seq(5, 10): _*)
> res5: String = 5 10
>
> scala> "%d %d".format(Array(5, 10): _*)
> java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
>
> But this works:
>
> scala> "%d %d".format(Array[Integer](5, 10): _*)
> warning: there were deprecation warnings; re-run with -deprecation for
> details
> res7: String = 5 10
>
> As does:
>
> scala> "%d %d %d".format((Array(1,2,3): Seq[Int]) : _*)
> res8: String = 1 2 3
>
> I'm not sure what exactly is expected to work. But you shouldn't be
> surprised Option doesn't.
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: f(Some(3):_*) //not work :-)

On Thu, Apr 15, 2010 at 10:02:16AM -0700, Razvan Cojocaru wrote:
> I guess my point was...since Option has a toList and Some(3).toList:_*
> works (obviously), why wouldn't :_* work directly on Option...or
> anything that has a toList for that matter?

Automatically converting things to other things is always a tradeoff
between convenience and type safety. In this case you don't give up
much in the way of type safety, but you give up other things (like some
simplicity of spec and implementation.)

A different way of looking at it is that the implicit on Option could as
easily be optionToSeq as optionToIterable (it's not like we don't know
what order those 0 or 1 elements come in) and then this would work with
no changes. And I don't know of any argument against that, but often
changing an implicit reveals implications (implicitations?) which I
don't see up front.

Razvan Cojocaru 2
Joined: 2009-11-20,
User offline. Last seen 42 years 45 weeks ago.
Re: f(Some(3):_*) //not work :-)

Indeed, implicits should not be the way to do it...it would work better if it
were like a "method" or "operator" which we could just add to
Option...anyways, enough said on the topic :)

thanks

Paul Phillips-3 wrote:
>
> On Thu, Apr 15, 2010 at 10:02:16AM -0700, Razvan Cojocaru wrote:
>> I guess my point was...since Option has a toList and Some(3).toList:_*
>> works (obviously), why wouldn't :_* work directly on Option...or
>> anything that has a toList for that matter?
>
> Automatically converting things to other things is always a tradeoff
> between convenience and type safety. In this case you don't give up
> much in the way of type safety, but you give up other things (like some
> simplicity of spec and implementation.)
>
> A different way of looking at it is that the implicit on Option could as
> easily be optionToSeq as optionToIterable (it's not like we don't know
> what order those 0 or 1 elements come in) and then this would work with
> no changes. And I don't know of any argument against that, but often
> changing an implicit reveals implications (implicitations?) which I
> don't see up front.
>

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