Design decision: echo(String*) chokes on Array[String]

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

Design decision: echo(String*) chokes on Array[String]

PerfectTiling
I'm curious about a design decision in Scala.  I wonder if someone might be able to enlighten me.

If we define a var-args echo, taking String*, as in

  def echo(args: String*) = for (arg <- args) println(arg)

then the compiler chokes if we feed it an Array[String].  Why is it this way?

As a naive beginner to Scala (and programming language design in general), it would seem that if you feed a typed function such as echo a typed array, it should be able to say "aha, I want to consume a list of Strings; I've been presented with an Array[String]; I know what I should do".  This would seem to an obvious benefit of strong typing: we know what echo is looking for, and we know what it's been given, and there's only one way to reconcile the two.

Can anyone tell me why is Scala designed this way?

[This echo example taken from page 154 of Odersky/Spoon/Venners.]
Reply | Threaded
Open this post in threaded view
|

Re: Design decision: echo(String*) chokes on Array[String]

David Hall-17

Sure, but what if it's def echo[A](args: A*) ? Do you unpack an array, or did the user mean to pass in only one object? It's easier to be consistent.

Fwiw, you can say myArray:_* to tell the compiler you mean to unpack the array.

-- David

On Dec 30, 2009 11:18 PM, "PerfectTiling" <[hidden email]> wrote:


I'm curious about a design decision in Scala.  I wonder if someone might be
able to enlighten me.

If we define a var-args echo, taking String*, as in

 def echo(args: String*) = for (arg <- args) println(arg)

then the compiler chokes if we feed it an Array[String].  Why is it this
way?

As a naive beginner to Scala (and programming language design in general),
it would seem that if you feed a typed function such as echo a typed array,
it should be able to say "aha, I want to consume a list of Strings; I've
been presented with an Array[String]; I know what I should do".  This would
seem to an obvious benefit of strong typing: we know what echo is looking
for, and we know what it's been given, and there's only one way to reconcile
the two.

Can anyone tell me why is Scala designed this way?

[This echo example taken from page 154 of Odersky/Spoon/Venners.]

--
View this message in context: http://old.nabble.com/Design-decision%3A-echo%28String*%29-chokes-on-Array-String--tp26975522p26975522.html
Sent from the Scala - Debate mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: Design decision: echo(String*) chokes on Array[String]

Paul Phillips-3
In reply to this post by PerfectTiling
On Wed, Dec 30, 2009 at 09:18:01PM -0800, PerfectTiling wrote:
> As a naive beginner to Scala (and programming language design in
> general), it would seem that if you feed a typed function such as echo
> a typed array, it should be able to say "aha, I want to consume a list
> of Strings; I've been presented with an Array[String]; I know what I
> should do".

Whenever I'm in a jam, when it looks like all hope is lost, when
desperate times are calling loudly and shrilly for the desperate
measures they see as their entitlement, I ask myself: what would
String.format do?

  def format(args : Any*): String

And afterward, things never look quite so grim.

--
Paul Phillips      | Adultery is the application of democracy to love.
Future Perfect     |     -- H. L. Mencken
Empiricist         |
all hip pupils!    |----------* http://www.improving.org/paulp/ *----------
Reply | Threaded
Open this post in threaded view
|

Re: Design decision: echo(String*) chokes on Array[String]

Daniel Sobral
In reply to this post by PerfectTiling
Paul's and David's answer are very interesting. I have another outlook on the question, however. To me, converting an array into a varargs is a type casting. I dislike automatic type castings -- they open doors to bugs.

Just because you may want something, it doesn't mean you do want it. It might just be a bug.

On Thu, Dec 31, 2009 at 3:18 AM, PerfectTiling <[hidden email]> wrote:

I'm curious about a design decision in Scala.  I wonder if someone might be
able to enlighten me.

If we define a var-args echo, taking String*, as in

 def echo(args: String*) = for (arg <- args) println(arg)

then the compiler chokes if we feed it an Array[String].  Why is it this
way?

As a naive beginner to Scala (and programming language design in general),
it would seem that if you feed a typed function such as echo a typed array,
it should be able to say "aha, I want to consume a list of Strings; I've
been presented with an Array[String]; I know what I should do".  This would
seem to an obvious benefit of strong typing: we know what echo is looking
for, and we know what it's been given, and there's only one way to reconcile
the two.

Can anyone tell me why is Scala designed this way?

[This echo example taken from page 154 of Odersky/Spoon/Venners.]

--
View this message in context: http://old.nabble.com/Design-decision%3A-echo%28String*%29-chokes-on-Array-String--tp26975522p26975522.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Daniel C. Sobral

I travel to the future all the time.