Automatic conversion between methods and functions in Scala

Posted by fikovnik on Stack Overflow See other posts from Stack Overflow or by fikovnik
Published on 2013-06-26T15:25:38Z Indexed on 2013/06/27 4:22 UTC
Read the original article Hit count: 97

Filed under:
|
|

I would like to understand the rules when can Scala automatically convert methods into functions. For example, if I have following two methods:

def d1(a: Int, b: Int) {}
def r[A, B](delegate: (A, B) ? Unit) {}

I can do this:

r(d1)

But, when overloading r it will no longer work:

def r[A, B, C](delegate: (A, B, C) ? Unit) {}
r(d1) // no longer compiles

and I have to explicitly convert method into partially applied function:

r(d1 _)

Is there any way to accomplish following with the explicit conversion?

def r[A, B](delegate: (A, B) ? Unit) {}
def r[A, B, C](delegate: (A, B, C) ? Unit) {}

def d1(a: Int, b: Int) {}
def d2(a: Int, b: Int, c: Int) {}

r(d1) // only compiles with r(d1 _)
r(d2) // only compiles with r(d2 _)

There is somewhat similar question, but it is not fully explained.

© Stack Overflow or respective owner

Related posts about function

Related posts about scala