Commit 854627a
authored
Clarify discard patterns (#1688)
* Clarify discard patterns
pecify that when the lone token `_` could parse as a `discard_pattern` but `_` also resolves to an accessible declaration in scope (a constant or a type), `_` is interpreted as a reference to that declaration (i.e. a `constant_pattern` or a type reference for the `is` operator) rather than as a `discard_pattern`. This preserves backward compatibility with code that defined `_` (typically a type) prior to discard support.
1. **Edit `standard/patterns.md` §11.2.7 (Discard pattern)** — after the grammar block, before/around the existing compile-time error sentence.
2. **Add a `*Note*`** under §11.2.7 contrasting with §11.2.2's `discard_designation` rule: the `_` in `Type _` is *always* a `discard_designation` (the in-scope `_` is hidden), whereas a standalone `_` pattern *is* hidden by an in-scope `_` for compatibility.
3. **Add a small `*Example*`** under §11.2.7 showing a `switch_expression` with an in-scope type/constant named `_`, demonstrating that the arm `_ => …` is interpreted against that name (e.g. as a type or constant pattern), not as a discard. Use `// compile-time warning expected` comment if applicable; pick a template such as `standalone-console` consistent with the surrounding examples. Keep it minimal.
4. **Cross-reference touch-up in §11.2.2**: append a brief parenthetical or `*Note*` stating "Contrast with §11.2.7, where a standalone `_` pattern is interpreted as a reference to an in-scope declaration named `_` if one exists."
* Respond to feedback.
is-type for type binding: The demotion rule is now a bulleted list. The type bullet explicitly says that relational_expression is _ is interpreted as the is-type operator (§12.14.12.1) when _ resolves to a type, and notes that a bare _ resolving to a type is not a valid pattern in other pattern contexts (it can still appear as the type of a declaration_pattern, e.g., _ x).
"demotes" wording removed: The *Note* now reads "… for _ an in-scope constant or type causes _ to be interpreted as a reference to that declaration rather than producing an error."
Scope clarified: Added an explicit sentence — "If _ resolves to anything other than an accessible constant or type (for example, a local variable, parameter, field, or method), the rule does not apply and _ remains a discard_pattern." The error sentence now reads "If, after applying the preceding rule, the token _ is still a discard_pattern, it is a compile-time error…", making it unambiguous that the is/switch_label error always applies whenever _ remains a discard.
Example refactored: Now uses two static methods WithoutUnderscore(int n) and WithUnderscore(int n), both returning string. No nested block, no s1/s2, consistent indentation.
* Clarify discard rule
The `_` can't be a top-level pattern, but can be a subpattern (in a positional pattern, or a parenthesized pattern).1 parent e48adcc commit 854627a
1 file changed
Lines changed: 37 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
371 | 373 | | |
372 | 374 | | |
373 | 375 | | |
374 | | - | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
375 | 386 | | |
376 | 387 | | |
377 | 388 | | |
| |||
408 | 419 | | |
409 | 420 | | |
410 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
411 | 447 | | |
412 | 448 | | |
413 | 449 | | |
| |||
0 commit comments