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

Why is the compiler complaining about default argument values?

3 replies
Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
In a class, the following three method signatures are defined:
def insert(at: Int, insertee: Mark, direction: Direction = Forward): Mark = ... def insert(at: Mark, insertee: Mark, direction: Direction = Forward): Mark = ...
def insert(at: Mark, content: Content, direction: Direction);

The types of arguments ensure that there can be no possible ambiguity as to which is called (there is no inheritance between Content and Mark). However, the compiler says:
    error: in trait Element, multiple overloaded alternatives of method insert define default arguments.trait Element {
Why is this a problem? There's no erasure happening, so I don't see why there would be any run-time ambiguity.
Thanks,Ken
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Why is the compiler complaining about default argument valu

unfortunately you cannot mix overloading and default arguments... (well, you can have _one_ method with defaults, but not more than one). this was done for simplicity, even if it would be possible to allow both overloading and multiple default arguments.

http://stackoverflow.com/questions/4652095/why-does-the-scala-compiler-d...

best, -sciss-

On 1 Feb 2012, at 01:04, Ken McDonald wrote:

> In a class, the following three method signatures are defined:
>
> def insert(at: Int, insertee: Mark, direction: Direction = Forward): Mark = ...
> def insert(at: Mark, insertee: Mark, direction: Direction = Forward): Mark = ...
> def insert(at: Mark, content: Content, direction: Direction);
>
> The types of arguments ensure that there can be no possible ambiguity as to which is called (there is no inheritance between Content and Mark). However, the compiler says:
>
> error: in trait Element, multiple overloaded alternatives of method insert define default arguments.
> trait Element {
>
> Why is this a problem? There's no erasure happening, so I don't see why there would be any run-time ambiguity.
>
> Thanks,
> Ken

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Why is the compiler complaining about default argument valu

Note that the original version of the code allowed multiple overloads
with default arguments. It was then changed to the single overload
restriction based on the experience with the former.

On Tue, Jan 31, 2012 at 23:08, Sciss wrote:
> unfortunately you cannot mix overloading and default arguments... (well, you can have _one_ method with defaults, but not more than one). this was done for simplicity, even if it would be possible to allow both overloading and multiple default arguments.
>
> http://stackoverflow.com/questions/4652095/why-does-the-scala-compiler-d...
>
> best, -sciss-
>
>
> On 1 Feb 2012, at 01:04, Ken McDonald wrote:
>
>> In a class, the following three method signatures are defined:
>>
>>       def insert(at: Int, insertee: Mark, direction: Direction = Forward): Mark = ...
>>       def insert(at: Mark, insertee: Mark, direction: Direction = Forward): Mark = ...
>>       def insert(at: Mark, content: Content, direction: Direction);
>>
>> The types of arguments ensure that there can be no possible ambiguity as to which is called (there is no inheritance between Content and Mark). However, the compiler says:
>>
>>     error: in trait Element, multiple overloaded alternatives of method insert define default arguments.
>> trait Element {
>>
>> Why is this a problem? There's no erasure happening, so I don't see why there would be any run-time ambiguity.
>>
>> Thanks,
>> Ken
>

Ken McDonald
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Why is the compiler complaining about default argument valu
Hmm, interesting. Good to know, at least.
Ken

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