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

Problem with implicit defs and overload resolution

2 replies
Tony Sloane
Joined: 2009-01-07,
User offline. Last seen 2 years 32 weeks ago.

I'm having a problem with interaction between an implicit def and
overload resolution that I can't work out. Can someone tell me what
I'm missing?

Consider this simplified case:

object Main extends Application {

case class State[T] (init : T) {
var value : T = init
}

implicit def stateTToT[T] (t : State[T]) : T = t.value
// implicit def stateTToT (t : State[Int]) : Int = t.value

val s = State[Int] (1)
val i : Int = 2 + s
println (i)

}

As written, the 2.7.3 Scala compiler complains:
Main.scala:11: error: ambiguous reference to overloaded definition,
both method + in class Int of type (Char)Int
and method + in class Int of type (Short)Int
match argument types (Main.State[Int]) and expected result type Int
val i : Int = 2 + s
^

If I use the second implicit def instead of the first (i.e.,
constraining it to only apply at Int), then there is no problem.
That's not a general solution, since I need non-Int State.

I'm at a loss to explain how Char and Short enter into the equation.
A State[Int] is not compatible with either of those types (eg. val c :
Char = s is illegal) so I don't see how the (Char) Int and (Short) Int
versions of + come to be considered.

Thanks for any light anyone can shed.

cheers,
Tony

Kristian Domagala
Joined: 2009-01-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Problem with implicit defs and overload resolution

I've come across something similar in the past, and ended up resorting
to something like this to help the compiler:

val i : Int = 2 + (s:Int)

If anyone knows of a better way, I'd be interested to hear it too.

Cheers,
Kristian.

On Thu, Jan 29, 2009 at 9:28 AM, Tony Sloane wrote:
> I'm having a problem with interaction between an implicit def and overload
> resolution that I can't work out. Can someone tell me what I'm missing?
>
> Consider this simplified case:
>
> object Main extends Application {
>
> case class State[T] (init : T) {
> var value : T = init
> }
>
> implicit def stateTToT[T] (t : State[T]) : T = t.value
> // implicit def stateTToT (t : State[Int]) : Int = t.value
>
> val s = State[Int] (1)
> val i : Int = 2 + s
> println (i)
>
> }
>
> As written, the 2.7.3 Scala compiler complains:
> Main.scala:11: error: ambiguous reference to overloaded definition,
> both method + in class Int of type (Char)Int
> and method + in class Int of type (Short)Int
> match argument types (Main.State[Int]) and expected result type Int
> val i : Int = 2 + s
> ^
>
> If I use the second implicit def instead of the first (i.e., constraining it
> to only apply at Int), then there is no problem. That's not a general
> solution, since I need non-Int State.
>
> I'm at a loss to explain how Char and Short enter into the equation. A
> State[Int] is not compatible with either of those types (eg. val c : Char =
> s is illegal) so I don't see how the (Char) Int and (Short) Int versions of
> + come to be considered.
>
> Thanks for any light anyone can shed.
>
> cheers,
> Tony
>
>

Tony Sloane
Joined: 2009-01-07,
User offline. Last seen 2 years 32 weeks ago.
Re: Problem with implicit defs and overload resolution

Yes, this is a work-around, but it would be nice to get to the bottom
of the real issue.

thanks,
Tony

On 29/01/2009, at 12:20 PM, Kristian Domagala wrote:

> I've come across something similar in the past, and ended up resorting
> to something like this to help the compiler:
>
> val i : Int = 2 + (s:Int)
>
> If anyone knows of a better way, I'd be interested to hear it too.
>
> Cheers,
> Kristian.
>
> On Thu, Jan 29, 2009 at 9:28 AM, Tony Sloane
> wrote:
>> I'm having a problem with interaction between an implicit def and
>> overload
>> resolution that I can't work out. Can someone tell me what I'm
>> missing?
>>
>> Consider this simplified case:
>>
>> object Main extends Application {
>>
>> case class State[T] (init : T) {
>> var value : T = init
>> }
>>
>> implicit def stateTToT[T] (t : State[T]) : T = t.value
>> // implicit def stateTToT (t : State[Int]) : Int = t.value
>>
>> val s = State[Int] (1)
>> val i : Int = 2 + s
>> println (i)
>>
>> }
>>
>> As written, the 2.7.3 Scala compiler complains:
>> Main.scala:11: error: ambiguous reference to overloaded definition,
>> both method + in class Int of type (Char)Int
>> and method + in class Int of type (Short)Int
>> match argument types (Main.State[Int]) and expected result type Int
>> val i : Int = 2 + s
>> ^
>>
>> If I use the second implicit def instead of the first (i.e.,
>> constraining it
>> to only apply at Int), then there is no problem. That's not a
>> general
>> solution, since I need non-Int State.
>>
>> I'm at a loss to explain how Char and Short enter into the
>> equation. A
>> State[Int] is not compatible with either of those types (eg. val
>> c : Char =
>> s is illegal) so I don't see how the (Char) Int and (Short) Int
>> versions of
>> + come to be considered.
>>
>> Thanks for any light anyone can shed.
>>
>> cheers,
>> Tony
>>
>>

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