Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala overloaded method value cannot be applied

Tags:

scala

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)
like image 315
宇宙人 Avatar asked Aug 01 '16 12:08

宇宙人


1 Answers

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

like image 123
hasumedic Avatar answered Nov 13 '22 10:11

hasumedic