I'm rather new to Scala and stumbled upon a small little issue that keeps bothering me. Let's say there is some method with default parameter
def foo(v: Any = "default"): String = s"called with parameter '$v'"
and an Option val opt: Option[String]
.
How to call this method with either the option value (if defined) or the default parameter?
I mean despite the obvious solution
val result = if (opt.isDefined)
from.here.to.foo(opt.get)
else
from.here.to.foo()
and having to type the method with (possibly long) object chain twice? Not to mention having more than one optional/default parameter...
All I could come up with is the unhelpful helper
def definedOrDefault[A, B](opt: Option[A], f0: => B, f1: A => B): B =
if (opt.isDefined) f1(opt.get) else f0
but when not being able to mention default parameters in higher order functions... that's it. Reminds me of the bad old days with Java where method overloading creates the same problem.
It seems like you are using two concepts of value being optional in one place, i.e. optional parameter and Option
. They don't like play well together, maybe it is better to use just one.
If you always just pass value of Option
to a method or pass nothing to get default value, maybe consider changing the function to accept the Option
.
def foo(v: Option[String]) = {
val param = v getOrElse "default"
s"called with parameter '$param'"
}
If you still want to have default parameter you can change signature to
def foo(v: Option[String] = None)
This approach however will require you to wrap your parameter into Some
when you do a regular call e.g.
foo(Some("regular param"))
but it works well when you use Option
s. You can easly add more optional parameters as well.
Here is an example
def foo(v1: Option[String] = None, v2: Option[Int] = None) = {
val param1 = v1 getOrElse "default"
val param2 = v2 getOrElse 42
s"'$param1', '$param2'"
}
foo() // "default", 42
foo(v2 = Some(12)) // "default", 12
foo(Some("asd"), Some(11)) // "asd", 11
val optionFromSomewhere = Some("a")
val anotherOptionFromSomewhere = Option.empty[Int]
foo(optionFromSomewhere, anotherOptionFromSomewhere) // "a", 42
You could also introduce implicit conversion from Any
to Option
, that would let you to omit Some
, but I don't think it is such a good idea
implicit def any2option[A](e: A): Option[A] = Some(e)