[scala] implicits problem

View: New views
2 Messages — Rating Filter:   Alert me  

[scala] implicits problem

by daniel mahler :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The following seems to demonstrates a bug in type inference for implicits:

Welcome to Scala version 2.8.0.r18965-b20091008021654 (OpenJDK 64-Bit
Server VM, Java 1.6.0_0).
Type in expressions to have them evaluated.
Type :help for more information.

scala>     class A;
defined class A

scala>     class B;
defined class B

scala>     implicit def a = Seq.empty[A]
a: Seq[A]

scala>     implicit def b[X <: B] = Seq.empty[X]
b: [X <: B]Seq[X]

scala>     def show[X](implicit x : X) = println(x)
show: [X](implicit x: X)Unit

scala>     show[Seq[A]]
<console>:10: error: ambiguous implicit values:
 both method b in object $iw of type [X <: B]Seq[X]
 and method a in object $iw of type => Seq[A]
 match expected type Seq[A]
           show[Seq[A]]
               ^

But there should be no ambiguity since:

scala>     b[A]
<console>:9: error: type arguments [A] do not conform to method b's
type parameter bounds [X <: B]
           b[A]
            ^
so b cannot be the implicit arg of show.

cheers
Daniel

Re: [scala] implicits problem

by Adriaan Moors-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is expected (unfortunately). Type inference infers(*) Nothing for b's type parameter (since it's covariant). When only b is in scope, you get:

show[Seq[A]](b[Nothing])

for a:

show[Seq[A]](a)

In other situations, it should work (if type inference actually fails for all but one alternative), see ticket 2421

adriaan

(*) you can observe the type checker's results using scalac -Xprint:typer

On Sat, Oct 10, 2009 at 3:39 AM, Daniel Mahler <dmahler@...> wrote:
The following seems to demonstrates a bug in type inference for implicits:

Welcome to Scala version 2.8.0.r18965-b20091008021654 (OpenJDK 64-Bit
Server VM, Java 1.6.0_0).
Type in expressions to have them evaluated.
Type :help for more information.

scala>     class A;
defined class A

scala>     class B;
defined class B

scala>     implicit def a = Seq.empty[A]
a: Seq[A]

scala>     implicit def b[X <: B] = Seq.empty[X]
b: [X <: B]Seq[X]

scala>     def show[X](implicit x : X) = println(x)
show: [X](implicit x: X)Unit

scala>     show[Seq[A]]
<console>:10: error: ambiguous implicit values:
 both method b in object $iw of type [X <: B]Seq[X]
 and method a in object $iw of type => Seq[A]
 match expected type Seq[A]
          show[Seq[A]]
              ^

But there should be no ambiguity since:

scala>     b[A]
<console>:9: error: type arguments [A] do not conform to method b's
type parameter bounds [X <: B]
          b[A]
           ^
so b cannot be the implicit arg of show.

cheers
Daniel


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm