- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Can't mix styles when supplying repeated parameters
Tue, 2011-01-25, 14:35
>>>>> "Gabor" == Bakos Gábor writes:
Gabor> One obvious reason would be to make no special case for the
Gabor> AnyRef* arguments. See the following def: def foo(args:
Gabor> AnyRef*)//1 With which arguments would you call in this case?
Gabor> foo("", Array("", "") : _*) I guess this would make it three ""
Gabor> Strings (and not a String with a String array).
I see no ambiguity in this case. _* was supplied, so that forces the
three-String interpretation.
Gabor> But what if there is an overload like this: def foo(text:
Gabor> String, args: AnyRef*)//2 Would you consider selecting the most
Gabor> specific appliable (2), or would it be a compile error?
By allowing repeated parameters at all, Scala already has this same
ambiguity. For example:
scala> object O {
| def foo(x: String) = 1
| def foo(xs: String*) = 2
| }
defined module O
scala> O.foo("")
res1: Int = 1
scala> O.foo("", "")
res2: Int = 2
Here both overloads are applicable, but the ambiguity is resolved by
existing language in section 4.6.2 of the spec: "...if a method m with
type (p1: T1, ..., pn: Tn, ps: S*) U is applied to arguments(e1, ...,
ek) where k >= n..."
Unless I'm missing something, this same language ("where k >= n") also
resolves the ambiguity you're worried about.
I did open a ticket, by the way:
http://lampsvn.epfl.ch/trac/scala/ticket/4181