Curried Defaults

I can define the function as:

def print(n:Int, s:String = "blah") {} print: (n: Int,s: String)Unit 

I can call him:

 print(5) print(5, "testing") 

If I review above:

 def print2(n:Int)(s:String = "blah") {} print2: (n: Int)(s: String)Unit 

I can not call it one parameter:

 print2(5) <console>:7: error: missing arguments for method print2 in object $iw; follow this method with `_' if you want to treat it as a partially applied function print2(5) 

I need to provide both parameters. Is there any way around this?

+8
scala parameters currying
source share
1 answer

You cannot omit () with default arguments:

 scala> def print2(n:Int)(s:String = "blah") {} print2: (n: Int)(s: String)Unit scala> print2(5)() 

Although it works with implicits:

 scala> case class SecondParam(s: String) defined class SecondParam scala> def print2(n:Int)(implicit s: SecondParam = SecondParam("blah")) {} print2: (n: Int)(implicit s: SecondParam)Unit scala> print2(5) 
+17
source share

All Articles