If I omit the semicolon, this code doesn't compile.
def checkRadioButton(xml: DslBuilder): String => XmlTree = {
val inputs = top(xml).\\*(hasLocalNameX("input"));
{ (buttonValue: String) =>
// code omitted
}
}
My guess is that, without the semicolon, scalac thinks that the partial function is another argument to the \\*
method, instead of the return value. (It isn't actually a partial function, by the way, it's a total function.)
Can I do without the semicolon here? I've never had to use a semicolon at the end of a line before in Scala.
Just add a second newline, which apparently is equivalent to the semicolon.
Still, I'm not entirely happy with this, as it seems fragile.
Here is a simplification, explanation, and beautification.
Simplified,
This fails because the newline after the
7
is not taken as a semicolon, for the reason that it might be a function application; you might want a DSL where the brace is on the next line. Here is the littlenl
in the syntax of an arg with braces.Newline handling is described in 1.2 of the spec; a few spots like this one, where a single
nl
is accepted, are mentioned at the end of the section.(Two newlines doesn't work, which is why that also fixes your problem.)
Notice that a
nl
is not accepted in front of a paren, so the following works (though with only parens, you get only one expression for your function literal):In fact, the best edit for the problem code is not more braces but fewer:
The reason for this nice syntax is that your method body is already a block expression, and when the result expression of a block is a function literal, you can simplify.
The type of
x
is also redundant.And not surprisingly:
I’d write it like this instead: