5 Oct 21:09
ambiguous implicit conversion error
From: Jim McBeath <scala <at> j.jimmc.org>
Subject: ambiguous implicit conversion error
Newsgroups: gmane.comp.lang.scala
Date: 2008-10-05 19:11:04 GMT
Subject: ambiguous implicit conversion error
Newsgroups: gmane.comp.lang.scala
Date: 2008-10-05 19:11:04 GMT
//Set up two implicit conversions to Option[something]
implicit def optIntToString(n:Option[Int]) = n.get.toString
implicit def optStringToString(s:Option[String]) = s.get.toString
val s1:String = { val opt = Some(123); opt } //OK, returns "123"
val s2:String = Some(123) //compiler error, ambiguous implicit conversion
I get this error for the s2 assignment:
error: type mismatch;
found : Some[?A]
required: String
Note that implicit conversions are not applicable because they are ambiguous:
both method optStringToString in object $iw of type (Option[String])java.lang.String
and method optIntToString in object $iw of type (Option[Int])java.lang.String
are possible conversion functions from Some[?A] to String
<console>:6: error: type mismatch;
found : Int(123)
required: String with Int
val s2:String = Some(123)
Why is the compiler able to figure out how to convert to s1 but not to s2?
It seems to me that it has the same information available in both cases.
In the error case it says it found Some[?A], why didn't it find Some[Int]?
Is this a bug, or am I missing something about how type inference works?
I tried this in the interpreter using 2.7.1-final and 2.7.2-RC2 with
the same results.
--
Jim
(Continue reading)
RSS Feed