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

weird interaction between implicits and package

No replies
Swade Leather
Joined: 2009-01-31,
User offline. Last seen 42 years 45 weeks ago.
Hi all,
I'm having a strange problem with implicits that are defined at the package level. Any ideas why (see below for source + log, using scala 2.9.01, Java 1.6.0_26)?
------------------------------------------------------------ test.scala -------------------------------------------------------------------------
object `package` {   trait Score { def toString : String }  trait Test[+T <: Score] { def apply(s : String) : T }
  case class FT(f : Float) extends Score   implicit object FT extends Test[FT] { def apply(s : String) : FT = new FT(s.toFloat) }
  case class IT(i : Int) extends Score  implicit object IT extends Test[IT] { def apply(s : String) : IT = new IT(s.toInt) } }
class TT[+T <: Score](implicit val tb : Test[T]) {   def read(s : String) : T = tb(s)}
object Tester {  val tt = new TT[FT]   val r = tt.read("1.0")  r.toString }
compiling it:
$ scala-2.9.0.1/bin/scalac test.scala test.scala:17: error: ambiguous implicit values: both object FT of type object package.FT  and object FT of type object package.FT match expected type package.Test[package.FT]   val tt = new TT[FT]           ^one error found
This, however, works:
------------------------------------------------------------ test2.scala -------------------------------------------------------------------------
object Tester {  trait Score { def toString : String }  trait Test[+T <: Score] { def apply(s : String) : T }
  case class FT(f : Float) extends Score   implicit object FT extends Test[FT] { def apply(s : String) : FT = new FT(s.toFloat) }
  case class IT(i : Int) extends Score  implicit object IT extends Test[IT] { def apply(s : String) : IT = new IT(s.toInt) }   class TT[+T <: Score](implicit val tb : Test[T]) {    def read(s : String) : T = tb(s)  }
  val tt = new TT[FT]  val r = tt.read("1.0")   r.toString}
compiling it:
$ scala-2.9.0.1/bin/scalac test2.scala $

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