score:3

this is a method with a single parameter int named x, that returns an int (x + 1) where the return type of int is inferred by the compiler.

def incimplicit(x: int) = x + 1

this is the same exact method as above, except the return type is explicit, and not inferred.

def incimplicit(x: int): int =  x + 1

this is a parameterless method that returns a function1[int, int] (which is a function with one int parameter that returns an int).

def incanonymous = ((x : int) => x + 1)

i.e. the long signature looks like this:

def incanonymous: function1[int, int] = ((x : int) => x + 1)

or

def incanonymous: int => int = ((x : int) => x + 1)

functionally, they are pretty much the same. if an int => int is required as a parameter, say for def map(f: int => int), they will all work. when calling map(incimplicit), the first two (which are the same exact method, anyway) will be implicitly converted to functions by eta-expansion. map(incanonymous) will already have the correct signature, and would require no conversion.

incimplicit(x) and incanonymous(x) will always return the same result for any x. the only difference is that incimplicit is one method call, and incanonymous is a method call that returns a function, and then the apply method of the returned function is invoked. generally, you would not have a method that returns a function unless you had a very good reason to do so.


Related Query

More Query from same tag