Reputation: 6566
I have a function which builds a path like so:
def generatePath(arg1: String, arg2: String, arg3: String* = null) = {
Array(arg1, arg2, arg3).mkString("/")
}
The function should return something like this:
generatePath("data", "g1")
"data/g1"
generatePath("data", "g1", "hello")
"data/g1/hello"
generatePath("data", "g1", "hello", "world", "yes")
"data/g1/hello/world/yes"
val additionalPaths: Array[String] = Array[String]("yahoo", "awesome")
generatePath("data", "g1", additionalPaths: _*)
"data/g1/yahoo/awesome"
I can't figure out how to get it to work though. I do want it to be so that you can omit arg3, that arg3 can take any number of arguments, or a flattened Seq.
Upvotes: 0
Views: 38
Reputation: 4017
Just remove default value and it will work. String*
allows zero-length lists as well.
def generatePath(arg1: String, arg2: String, arg3: String* ) = {
Array(arg1, arg2, arg3.mkString("/")).mkString("/")
}
println(generatePath("1", "2"))
println(generatePath("1", "2", "3"))
println(generatePath("1", "2", "3", "4"))
Upvotes: 2