Scala default function literal parameter -


in scala possible provide default value parameter function?

for example, in code have this.

def noop(): unit = {}  def dosomethinggreat(succeed: boolean)(f: => unit)(default: => unit = noop): unit = {   if (success) {     f   } else {     default   } } 

when try calling dosomethinggreat , leave out parameter default, though, error saying didn't pass in enough parameter. help?

so far, workaround explicitly pass in no-op function third parameter, defeats purpose of having default there in first place...

you need add parenthesis method invocation , scala pick default function:

scala>  def noop(): unit = { println(567) } noop: ()unit  scala>   def dosomethinggreat(succeed: boolean)(f: => unit)(default: => unit = noop): unit = {      |     if (succeed) {      |       f      |     } else {      |       default      |     }      |   } dosomethinggreat: (succeed: boolean)(f: => unit)(default: => unit)unit  scala>   dosomethinggreat(succeed = true)(println(123))() 123  scala>   dosomethinggreat(succeed = false)(println(123))() 567 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -