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
Post a Comment