14

I am trying to set a default value to an anonymous function in scala and so for not able to find any solution. Hope some one would help me out in SO.

I have the following structure,

case class A(id:Int = 0)

case class B(a:A)

object B {
     def func1(f:Int = 0)={
      ........
     }
 def func2(f:A => B = (how to give default value ?))={
        case Nothing => {
         //do something....
        }
        case _ => {
         //do some other thing......
        }
 }
} 

Basically, I want to make passing the parameter as optional. How can I achieve this?

3 Answers 3

18

Like any other default parameter:

scala> def test(f: Int => Int = _ + 1) = f
test: (f: Int => Int)Int => Int

scala> test()(1)
res3: Int = 2

or with String:

scala> def test(f: String => String = identity) = f
test: (f: String => String)String => String

scala> test()
res1: String => String = <function1>

scala> test()("Hello")
res2: String = Hello

Edit:

In case if you want to use a function provided by default, you have to use () explicitly, either Scala won't paste a default argument.

If you don't wanna use a default function and provide an explicit one, just provide it yourself:

scala> test(_.toUpperCase)("Hello")
res2: String = HELLO
Sign up to request clarification or add additional context in comments.

3 Comments

I wonder how this answer relates to the question, it seems to be about use of curried functions rather than about default parameters?
OK, got it. But i guess your example would be more instructive if it had an example for the non-default case, e.g. test(_*33)(1)
@GyroGearless thanks, added one more example to be more clear
1

Use an implicit parameter. Place an implicit value for the parameter in the object. This will be used unless you provide an explicit parameter or you have provided another implicit value in the calling scope.

case class A(id:Int = 0)

case class B(a:A)

object B {
  implicit val defFunc: A => B = {a: A =>  new B(a) }
  def func1(f:Int = 0)={
  }
  def func2(implicit func: A => B) = { ... }
} 

The differences between this method and Alexlv's method are

  1. This works with standalone functions as well as methods.
  2. The scope rules allow for providing appropriate overrides in appropriate scopes. Alex's method would require subclassing or eta-expansion (with partial application) to change the default.

I offer this solution since you are already using an object. Otherwise, Alexvlv's example is simpler.

2 Comments

Your func2 method is very ungly in the sense of its usage. You take two param lists and return a partial function, so if you try to call the result function in place this would be not what you would expect, i.e call func2(10) (the second branch with match all _) will throw an error cause func2 expect an implicit param and you gave it an int. I can give you tons of examples from production, when implicits (with a design close to your) are a disaster and breaks much.
True. Was letting myself be hampered by trying to leave his original code mostly as is. Edited for sanity.
0

The other answers show how to provide some existing default value, but if you want the default to do nothing (as suggested by case Nothing) then you can use Option/None.

 def func2(f:Option[A => B] = None)={
    case Some(f) =>
      //do something....
    case None =>
      //do some other thing......
 }


 func2()
 func2( Some(_ + 1) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.