Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, can you make an anonymous function have a default argument?

This works:

scala>  def test(name: String = "joe"): Boolean = true 
test: (name: String)Boolean

I expected this to work in the same way:

scala>  val test: String => Boolean = { (name: String = "joe") => true }

console>:1: error: ')' expected but '=' found.
like image 311
Chandler Avatar asked Aug 11 '14 01:08

Chandler


People also ask

Can a function argument have default value?

Any number of arguments in a function can have a default value.

How do I set default value in Scala?

You can assign a default value for a parameter using an equal to symbol. I gave a default value for the first parameter. If the caller doesn't pass any value for the first parameter, Scala will take the default value as println. That's it.

What is an anonymous function Scala?

In Scala, An anonymous function is also known as a literal function. A function that does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function.

What is a way that default arguments can be provided to a function?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.


3 Answers

The boring, correct answer is no, you can't, but actually you kind of can, with the experimental single abstract method (SAM) synthesis in 2.11.

First you need to define your own SAM type with the default value for the apply method's parameter:

trait Func {
  def apply(name: String = "joe"): Boolean
}

Now you can use the function literal notation to define a Func (note that you'll need to have started the REPL with -Xexperimental for this step to work):

val f: Func = { (name: String) => name == "joe" }

Or just:

val f: Func = _ == "joe"

And then the usual stuff:

scala> f("joe")
res0: Boolean = true

scala> f("eoj")
res1: Boolean = false

And the punchline:

scala> f()
res2: Boolean = true

It's not exactly the syntax you're asking for, and there are no promises that this will ever leave experimental status, and even if it does, default arguments may not be supported—but I still think it's pretty neat that it works now.

like image 175
Travis Brown Avatar answered Oct 03 '22 14:10

Travis Brown


To expand on "The boring, correct answer is no" in Travis Brown's answer:

Functions (i.e. objects of FunctionN[...] type) can't have default arguments in Scala, only methods can. Since anonymous function expressions produce functions (and not methods), they can't have default arguments.

like image 27
Alexey Romanov Avatar answered Oct 03 '22 14:10

Alexey Romanov


This is bit dirty solution I think, using currying

def testDef(nameDef: String)(name2: String): Boolean = { 
  val name3 = if ( name2 != "") name2 else nameDef 
   //use name3 for your logic 
    true 
  }

//> testDef: (name: String)(name2: String)Boolean

Curry the testDef method to hold default value as shown below.

var test = test("joe")_   //> test  : String => Boolean=function1

test("fun") //"fun" will be used as name3  
test("") //  "joe" will be used as name3
like image 24
user3366706 Avatar answered Oct 03 '22 14:10

user3366706