[#1176] | project: compiler | priority: medium | category: bug | |
---|---|---|---|---|
submitter | assigned to | status | date submitted | |
Nikolay | Martin | noise | 2007-06-17 18:26:33.0 | |
subject | Type inference problem with overloading | |||
code |
class Test { def f(x: Int, y: Int): Int = x + y def f(x: Double, y: Double): Double = x + y def foobar(xs: List[Int]): Int = xs.reduceLeft(f) } |
|||
what happened | infer.scala:5: error: ambiguous reference to overloaded definition, both method f in class Test of type (Double,Double)Double and method f in class Test of type (Int,Int)Int match expected type (?, ?) => ? def foobar(xs: List[Int]): Int = xs.reduceLeft(f) ^ one error found |
|||
what expected | Compiler should infer f(Int, Int)Int as a precise match. BTW, throw in a third definition f(Float,Float)Float and f(Int,Int)Int doesn't even show up in the error message (only the Double and Float versions)! | |||
[back to overview] |
Martin edited on 2007-06-18 10:06:13.0 |
This behavior is actually as spec'ed. The problem is in the type of reduceLeft which is too polymorphic: def reduceLeft[B >: A](f: (B, B) => B): B So `f' is typed with expected type (?, ?) => ?. We can't tighten the type of `reduceLeft' either because that would violate variance restrictions of Lists type parameter A. |