Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method call with option value or default parameter in Scala

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.

like image 585
ABika Avatar asked Jun 10 '16 15:06

ABika


People also ask

What is the function parameter with a default value in Scala?

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" .

Is Scala call by reference or value?

Java and Scala both use call by value exclusively, except that the value is either a primitive or a pointer to an object. If your object contains mutable fields, then there is very little substantive difference between this and call by reference.

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() ...

How do I use options in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.


1 Answers

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 Options. 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)
like image 67
Łukasz Avatar answered Oct 08 '22 02:10

Łukasz