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

Second parameter list & Overloading, Bug or „normal“ behaviour?

1 reply
toivo
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.

class Over {

def fun(i: Int): Double = i

def fun(i: Int)(fn:Int=>Double): Double = fn(i)
}

object OverTester {
def main(args : Array[String]): Unit = {

val over = new Over
over.fun(5) // <- error
}
}

Compiler Error:
ambiguous reference to overloaded definition,
both method fun in class Over of type (Int)((Int) => Double)Double
and method fun in class Over of type (Int)Double
match argument types (Int) and expected result type Unit

But

class Over {

def fun(i: Int): Double = i

def fun(i: Int, fn:Int=>Double): Double = fn(i)
}

is OK?

Toivo

Robert Kosara
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Second parameter list & Overloading, Bug or „normal“ behavi
Well the compiler tells you exactly what the problem is: over.fun(5) could call the first function, returning a Double, or it could call the second function, returning a function that returns a Double. Read about function currying in Programming in Scala, it's all explained there.
BTW, your email address doesn't seem to work ...

On Thu, Dec 18, 2008 at 7:36 AM, toivo <tom.tad@mail.ee> wrote:

class Over {

 def fun(i: Int): Double = i

 def fun(i: Int)(fn:Int=>Double): Double = fn(i)
}

object OverTester {
 def main(args : Array[String]): Unit = {

   val over = new Over
   over.fun(5)          // <- error
 }
}

Compiler Error:
ambiguous reference to overloaded definition,
both method fun in class Over of type (Int)((Int) => Double)Double
and  method fun in class Over of type (Int)Double
match argument types (Int) and expected result type Unit

But

class Over {

 def fun(i: Int): Double = i

 def fun(i: Int, fn:Int=>Double): Double = fn(i)
}

is OK?


Toivo

--
View this message in context: http://www.nabble.com/Second-parameter-list---Overloading%2C-Bug-or-%E2%80%9Enormal%E2%80%9C-behaviour--tp21071904p21071904.html
Sent from the Scala - User mailing list archive at Nabble.com.


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