Description
In Scala 2, a typed pattern is always of the form x: T
. Scala 3 changed the syntax to pat: T
in #6919 to support syntax like 3956: BigInt
.
This feature pat: T
is potentially useful as argued by many. However, I'd like to argue that it's better to introduce it later:
- No books or courses depend on the syntax
- The practical use cases are still not clear
- Fix #10994: align typed pattern syntax with Scala 2 #11023 found that the usage in the compiler is in fact a misuse.
- The exhaustivity check currently does not support it (and would require changes)
- It's unclear whether it affects
isInstanceOf
checks - Bugs might be latent in typer (see nested named irrefutable typetest pattern causes exhaustivity warning #10994, align precedence of given pattern in typetest with a nested typetest lampepfl/dotty-feature-requests#159)
- We already identify inconsistencies here
As argued by C.A.R. Hoare in The Emperor's Old Clothes [1]:
When any new language design project is nearing completion, there is always a mad rush to get new features added before standardization. The rush is mad indeed, because it leads into a trap from which there is no escape. A feature which is omitted can always be added later, when its design and its implications are well understood. A feature which is included before it is fully understood can never be removed later.
While generalizing to pat: T
from x: T
might make specification simpler, it makes correct implementation more complex (bug #10994). To quote [1] again:
The price of reliability is the pursuit of the utmost simplicity.
Other context
- PR Fix #10994: align typed pattern syntax with Scala 2 #11023 tries to align the pattern syntax closer to
x: T
. - A related change of the pattern syntax from
_*
topat: _*
happend in Use p* for vararg splices #11240. It causes problem (Pattern sequence 'Pattern2*' is allowed by grammar, but behaves strange #11457) and gets reverted in Vararg patterns must be variable patterns or wildcards #11508.
References
- The Emperor's Old Clothes, C.A.R. Hoare, 1981