dotty-feature-requests
dotty-feature-requests copied to clipboard
Boost binding precedence vs infix in patterns
This transfers https://github.com/scala/bug/issues/10821
Take
case head @ P(x) :: Nil =>
as
case (head @ P(x)) :: Nil =>
instead of as
case head @ (P(x) :: Nil) =>
by analogy to pattern alternatives
case matched @ (X | Y | Z) =>
Current usage suggests that parens are rarely omitted:
case alts @ (hd :: _) =>
case (t @ ValFrom(pat, rhs)) :: (rest @ (ValFrom(_, _) :: _)) =>
is written instead of currently legal
case alts @ hd :: _ =>
case (t @ ValFrom(pat, rhs)) :: (rest @ ValFrom(_, _) :: _) =>
which suggests that the current precedence is not natural or intuitive or easy to scan.
For reasons of compatibility and migration, this change would be enabled under -strict
language rules for 3.1/3.2. Fully parenthesized patterns are not affected.
What would a migration scenario look like exactly? Can we ensure that there is never a silent change in meaning?
as
with usual precedence is merged.
... and unmerged.
Probably this tweak makes more sense at 3.0, with a migration warning, since adding parens will always cross-compile.
In summary, higher precedence means that all
binds the first pattern and the parens around more
are not required.
case all @ x R y =>
case all @ d(n) :: (more @ d(_)) :: d(_) :: Nil =>
case all @ d(n) :: d(more @ _) :: d(_) :: Nil =>
becomes
case all @ (x R y) =>
case left @ x R y =>
case head @ d(n) :: next @ d(_) :: d(_) :: Nil =>
Currently, omitting the parens for more
is not valid syntax: it does not mean bind the rest of the pattern.