Well, what if we start from
foo match
case Foo(str.startsWith("hello"), int < 0) => str.dropRight(-int)
That clearly would break existing rules even worse, and isnât fixable by using some other term (e.g. .
) in place of _
. I donât like that the magic expansion canât be used unless you never need to speak of the terms again.
At least with ordinary underscore expansion, the rewriting is in-place.
_.length + _.size
(x, y) => x.length + y.size
But with this, the rewriting involves moving code around considerably more, including changing apparent scope (in vs out of ()
):
case Foo( _.length > 0, _.isDefined) =>
case Foo(x, y) if x.length > 0 && y.isDefined =>
So I think if it was going to work, weâd need an intermediate expansion that would allow declaration as well. For instance, akin to @Sporarumâs idea:
{: x if x.startsWith("Hello") :}
where {:
and :}
is a âpermit variable binding only when in this setâ operation, and could be elided if it was syntactically clear without. Iâll assume hereafter that it would be syntactically unambiguous, though Iâm not certain.
Then at least it would simply be direct shorthand:
case Foo( _.startsWith("hello"), _ < 0) =>
case Foo(x if x.startsWith("hello"), y if y < 0) =>
But honestly, even then I donât like it much. It seems irregular and provides minimal savings, and Iâm not sure it reads better even to an expert, and to a non-expert Iâm pretty sure itâs harder to read.
Indeed, I might support the embedded if syntax without the _
syntax, because that is kind of nice in that reasoning can be more local. (But it is kind of not-nice in that it makes it harder to pick out which new term names are being introduced.)