I'm running into a weird situation in some Scala code I'm writing (more on why in
a later post), and I'm curious to know from my Scala-ish followers if this is a bug
or intentional/"by design".
First of all, I can define a function that takes a variable argument list, like so:
def varArgs(key:String, args:Any*) = {
println(key)
println(args)
true
}
varArgs("Howdy")
And this is good.>
I can also write a function that returns a function, to be bound and invoked, like
so:
val good1 = (key:String) => {
println(key)
true
}
good1("Howdy")
And this also works.>
But when I try to combine these two, I get an interesting error:
val bad3 = (key:String, args:Any*) => {
println(key)
println(args)
true
}
bad3("Howdy", 1, 2.0, "3")
... which yields the following compilation error:
Envoy.scala:169: error: ')' expected but identifier found.
val bad3 = (key:String, args:Any*) => {
^
one error found
... where the "^" is lined up on the "*" in the "args" parameter, in case the formatting
isn't clear.>
Now, I can get around this by using a named function and returning it as a partially-applied
function:
val good2 = {
def inner(key:String, args:Any*) = {
println(key)
println(args)
true
}
inner _
}
good2("Howdy", 1, 2.0, "3")
... but it's a pain. Can somebody tell me why "bad3", above, refuses to compile? Am
I not getting the syntax right here, or is this a legit bug in teh compiler?>
Enterprise consulting, mentoring or instruction. Java, C++, .NET or XML services.
1-day or multi-day workshops available. Contact
me for details [1].
Links:
[1] mailto:ted@tedneward.com