宇宙人
宇宙人

Reputation: 1227

scala overloaded method value cannot be applied

The following code works:

def bbb(v: Double => Unit)(a: Double): Unit = v(a)
bbb{v: Double => v == 0 }(5)
bbb{v: Double =>  Array(v) }(5)

But if I overload bbb as follows, it doesn't work unless I manually assign the type signature for the first bbb call:

def bbb(v: Double => Unit)(a: Double): Unit = v(a)
def bbb(v: Double => Array[Double])(a: Double): Array[Double] = v(a)
bbb{v: Double => v == 0 }(5) // bbb{(v => v == 0):(Double => Unit)}(5)
bbb{v: Double =>  Array(v) }(5)

Upvotes: 9

Views: 28799

Answers (1)

hasumedic
hasumedic

Reputation: 2167

I think this is related to implicit conversions. In the first case, when you only have a definition that results in a Unit, even though you get results such as Boolean or Array, an implicit conversion to Unit is triggered returning you always the expected Unit.

When you overload, this implicit conversion is no longer applied, but the overloading resolution mechanism instead. You can find how this works in more detail in the Scala Language Specification

Upvotes: 6

Related Questions