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

Making the scala compiler smarter about overloaded methods

3 replies
Hiram Chirino
Joined: 2011-02-01,
User offline. Last seen 42 years 45 weeks ago.

When you have a couple of simple overloaded methods like:

object Log {
def log(m: String) = println("1:"+m)
def log(m: String, args: Any*) = println("2:"+m)
}
Log.log("test")
Log.log("test", 1)

The scala compiler is smart enough pick the right method, for example:

1:test
2:test

But in case like this:

object Log {
def log(m: => String) = println("1:"+m)
def log(m: String, args: Any*) = println("2:"+m)
}
Log.log("test")
Log.log("test", 1)

I complains with:

error: ambiguous reference to overloaded definition,
both method log in object Log of type (m: String,args: Any*)Unit
and method log in object Log of type (m: => String)Unit
match argument types (java.lang.String)
Log.log("test")

My question is why is it smart enough in the first case, but not the
second?

Hiram Chirino
Joined: 2011-02-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Making the scala compiler smarter about overloaded methods

Anyone out there?

On Feb 1, 11:13 am, Hiram Chirino wrote:
> When you have a couple of simple overloaded methods like:
>
>   object Log {
>      def log(m: String) = println("1:"+m)
>      def log(m: String, args: Any*) = println("2:"+m)
>   }
>   Log.log("test")
>   Log.log("test", 1)
>
> The scala compiler is smart enough pick the right method, for example:
>
>   1:test
>   2:test
>
> But in case like this:
>
>   object Log {
>      def log(m: => String) = println("1:"+m)
>      def log(m: String, args: Any*) = println("2:"+m)
>   }
>   Log.log("test")
>   Log.log("test", 1)
>
> I complains with:
>
>   error: ambiguous reference to overloaded definition,
>   both method log in object Log of type (m: String,args: Any*)Unit
>   and  method log in object Log of type (m: => String)Unit
>   match argument types (java.lang.String)
>          Log.log("test")
>
> My question is why is it smart enough in the first case, but not the
> second?

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: Making the scala compiler smarter about overloaded meth

i have another question: why doesn't the compiler complain about the
first case as well? both methods match. there is no "right method".

Am 06.02.2011 14:37, schrieb Hiram Chirino:
> Anyone out there?
>
> On Feb 1, 11:13 am, Hiram Chirino wrote:
>> When you have a couple of simple overloaded methods like:
>>
>> object Log {
>> def log(m: String) = println("1:"+m)
>> def log(m: String, args: Any*) = println("2:"+m)
>> }
>> Log.log("test")
>> Log.log("test", 1)
>>
>> The scala compiler is smart enough pick the right method, for example:
>>
>> 1:test
>> 2:test
>>
>> But in case like this:
>>
>> object Log {
>> def log(m: => String) = println("1:"+m)
>> def log(m: String, args: Any*) = println("2:"+m)
>> }
>> Log.log("test")
>> Log.log("test", 1)
>>
>> I complains with:
>>
>> error: ambiguous reference to overloaded definition,
>> both method log in object Log of type (m: String,args: Any*)Unit
>> and method log in object Log of type (m: => String)Unit
>> match argument types (java.lang.String)
>> Log.log("test")
>>
>> My question is why is it smart enough in the first case, but not the
>> second?

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Re: Making the scala compiler smarter about overloaded meth

On Sun, Feb 6, 2011 at 3:11 PM, HamsterofDeath wrote:
> i have another question: why doesn't the compiler complain about the
> first case as well? both methods match. there is no "right method".
>

These are not questions about the compiler but about the language. The
language does have a spec that talks about these
things (section on overloading resolution). Both questions which have
been brought up here can be resolved by consulting the spec.
Any suggestion to do differently would have to propose a different
wording of the spec.

Cheers

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