Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default value for function parameter in scala

Tags:

lambda

scala

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?

like image 227
user809564 Avatar asked Sep 30 '13 08:09

user809564


People also ask

Can you assign the default values to a function parameters?

Default parameter in JavascriptThe default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

What is the default value for string in scala?

def aMethod(param: String = "asdf") = { ... } If the method is called as follows, then param is given the default value "asdf": aMethod() ...

What is the default Behaviour of scala method?

Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. The parameter level has a default value so it is optional. On the last line, the argument "WARNING" overrides the default argument "INFO" .

What is default parameter in Scala?

Scala - Default Parameter Values for a Function. Scala lets you specify default values for function parameters. The argument for such a parameter can optionally be omitted from a function call, in which case the corresponding argument will be filled in with the default.

Can I define a default value for a function parameter?

As this example shows, you can define a default value for a function parameter just like you can define a default value for an Int or String. That’s both cool, and useful.

What is the default value of defaultfilter in filter function?

The third method parameter is named filterFunction, and its signature says it must take a value of type Song and return a Boolean, AND it also says that defaultFilter is the default value for this parameter.

How do I set default values for method arguments?

Just as with constructor parameters, you can provide default values for method arguments. Because you have provided defaults, the consumer of your method can either supply an argument to override the default or skip the argument, letting it use its default value.


2 Answers

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
like image 157
4lex1v Avatar answered Oct 18 '22 12:10

4lex1v


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.

like image 41
itsbruce Avatar answered Oct 18 '22 12:10

itsbruce