Functions without arguments, with unit as argument in scala

Posted by scout on Stack Overflow See other posts from Stack Overflow or by scout
Published on 2010-05-05T15:21:52Z Indexed on 2010/05/05 16:28 UTC
Read the original article Hit count: 131

Filed under:
 def foo(x:Int, f:Unit=>Int) = println(f())

foo(2, {Unit => 3+4}

//case1
def loop:Int = 7
foo(2, loop) //does not compile

changing loop to 
//case 2
def loop():Int = 7
foo(2, loop) // does not compile

changing loop to
//case 3
def loop(x:Unit): Int = 7 //changing according to Don's Comments
foo(2,loop) // compiles and works fine

should'nt case 1 and case 2 also work? why are they not working?

defining foo as

def foo(x:Int, y:()=>Int)

then case 2 works but not case 1.

Arent they all supposed to work, defining the functions either way.

//also i think ()=>Int in foo is a bad style, y:=>Int does not work, comments??

© Stack Overflow or respective owner

Related posts about scala