dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

Boost binding precedence vs infix in patterns

Open som-snytt opened this issue 4 years ago • 3 comments

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.

som-snytt avatar May 17 '20 07:05 som-snytt

What would a migration scenario look like exactly? Can we ensure that there is never a silent change in meaning?

odersky avatar Jun 01 '20 13:06 odersky

as with usual precedence is merged.

som-snytt avatar Sep 25 '20 04:09 som-snytt

... 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.

som-snytt avatar Dec 01 '20 00:12 som-snytt