I am trying to create a function with the variable number of arguments
def foo(args: String*)
What this function does is, it eliminates the empty strings and separate rest of the strings with a comma (,
).
def foo(args: String*) = {
args.flatMap {
case str if str.isEmpty => None
case str => Some(str)
}.mkString(", ")
}
When I extended this function to support Option[String]
arguments
def foo(args: Any*) = {
args.flatMap {
case str: String if str.isEmpty => None
case str: Option[String] if str.getOrElse("").isEmpty => None
case str => Some(str)
}.mkString(", ")
}
I got a warning saying
warning: non-variable type argument String in type pattern Option[String] is unchecked since it is eliminated by erasure
And when I pass arguments
foo("", "Hello", Some(""), Some("what"))
I got error
scala.MatchError: Some(what) (of class scala.Some) at $anonfun$makeAddress$1.apply(:12) at $anonfun$makeAddress$1.apply(:12)
How should I create such function supporting Option[String]
as well?