joesan
joesan

Reputation: 15435

Is it possible to specify function as parameter's default value?

Let's say that I have a function as below:

def func(x: Int)(f: Int => Boolean) = f(x)

Is it possible to define a default value for the function?

def func(x: Int)(f: Int => Boolean = defaultFunc) = f(x)

Where defaultFunc is a function that goes from Int to Boolean. I will anyways try this out by myself, but just thought of posting it here!

Upvotes: 0

Views: 59

Answers (1)

om-nom-nom
om-nom-nom

Reputation: 62855

Yes, it's possible, as you could find out by yourself using REPL or scalafiddle:

def defaultFunc(x: Int): Boolean = ???
// defaultFunc: (x: Int)Boolean

def func(x: Int)(f: Int => Boolean = defaultFunc) = f(x)
// func: (x: Int)(f: Int => Boolean)Boolean

Upvotes: 6

Related Questions