score:2

Accepted answer

func is type a => b but it has a default value of "just return the a part". so you can invoke foo without an argument and it will use the default function.

score:0

this is called an optional parameter with a default value. basically,

def foo(x: int = 42) = ???

means: x is of type int and if the caller doesn't pass an argument, use 42 instead.

e.g.:

def foo(x: int = 42) = x

foo(23) //=> 23
foo()   //=> 42

as opposed to:

def bar(x: int) = x

bar(23) //=> 23
bar()   //error

score:1

as the others have written, the syntax is for a default parameter value, but i'd like to point out an interesting thing about its type. so the function takes a function from a to b and - if none provided it'll default to a basic identity function in a.

so how can a parameter of type a => b have a default value of type a => a?

strange as it may seem, but this compiles. calling it without a parameter passed however, will only compile if type parameters a and b are explicitly stated or inferred to be the same type.

so foo[string, int] should not, while foo[int, int] should compile if i'm not mistaken. i'm not sure what would happen about foo without explicit type params however, maybe a not very helpful [nothing, nothing] inferred?


Related Query

More Query from same tag