Skip to content

Commit a35e524

Browse files
eemelimacchiatiaphillips
authored
Select "Match on variables instead of expressions" for selection-declarations (#824)
* Select "Match on variables instead of expressions" for selection-declarations * Add hybrid option to selection-declaration.md (#870) * Add hybrid option to selection-declaration.md * Update selection-declaration.md fixed glitch in original edit * Update selection-declaration.md * Apply suggestions from code review Fixing typos Co-authored-by: Addison Phillips <addison@unicode.org> * Update selection-declaration.md * Update exploration/selection-declaration.md Co-authored-by: Eemeli Aro <eemeli@mozilla.com> * Update exploration/selection-declaration.md Co-authored-by: Eemeli Aro <eemeli@mozilla.com> * Update exploration/selection-declaration.md Co-authored-by: Eemeli Aro <eemeli@mozilla.com> --------- Co-authored-by: Addison Phillips <addison@unicode.org> Co-authored-by: Eemeli Aro <eemeli@mozilla.com> * Update selection-declaration.md --------- Co-authored-by: Mark Davis <mark@unicode.org> Co-authored-by: Addison Phillips <addison@unicode.org>
1 parent abff42c commit a35e524

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

exploration/selection-declaration.md

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Effect of Selectors on Subsequent Placeholders
22

3-
Status: **Proposed**
3+
Status: **Proposed, Ballot Requested**
44

55
<details>
66
<summary>Metadata</summary>
@@ -10,7 +10,8 @@ Status: **Proposed**
1010
<dt>First proposed</dt>
1111
<dd>2024-03-27</dd>
1212
<dt>Pull Requests</dt>
13-
<dd>#000</dd>
13+
<dd><a href="https://github.com/unicode-org/message-format-wg/pull/755">#755</a></dd>
14+
<dd><a href="https://github.com/unicode-org/message-format-wg/pull/824">#824</a></dd>
1415
</dl>
1516
</details>
1617

@@ -159,14 +160,11 @@ _What prior decisions and existing conditions limit the possible design?_
159160

160161
## Proposed Design
161162

162-
_Describe the proposed solution. Consider syntax, formatting, errors, registry, tooling, interchange._
163+
The design alternative [Match on variables instead of expressions](#match-on-variables-instead-of-expressions)
164+
described below is selected.
163165

164166
## Alternatives Considered
165167

166-
_What other solutions are available?_
167-
_How do they compare against the requirements?_
168-
_What other properties they have?_
169-
170168
### Do nothing
171169

172170
In this alternative, selectors are independent of declarations.
@@ -422,3 +420,69 @@ and a data model error otherwise.
422420
Removes some self-documentation from the pattern.
423421
- Requires the pattern to change if the selectors are modified.
424422
- Limits number of referenceable selectors to 10 (in the current form)
423+
424+
### Hybrid approach: Match may mutate, no duplicates
425+
426+
In this alternative, in a `.match` statement:
427+
428+
1. variables are mutated by their annotation
429+
2. no variable can be the operand in two selectors
430+
431+
This keeps most messages more concise, producing the expected results in Example 1.
432+
433+
#### Example 1
434+
435+
```
436+
.match {$count :integer}
437+
one {{You have {$count} whole apple.}}
438+
* {{You have {$count} whole apples.}}
439+
```
440+
is precisely equivalent to:
441+
442+
#### Example 2
443+
```
444+
.local $count2 = {$count :integer}
445+
.match {$count2}
446+
one {{You have {$count2} whole apple.}}
447+
* {{You have {$count2} whole apples.}}
448+
```
449+
450+
This avoids the serious problems with mismatched selection and formats
451+
as in Example 1 under "Do Nothing", whereby the input of `count = 1.2`,
452+
results the malformed "You have 1.2 whole apple."
453+
454+
Due to clause 2, this requires users to declare any selector using a `.input` or `.local` declaration
455+
before writing the `.match`. That is, the following is illegal.
456+
457+
#### Example 3
458+
```
459+
.match {$count <anything>}{$count <anything>}
460+
```
461+
It would need to be rewritten as something along the lines of:
462+
463+
#### Example 4
464+
```
465+
.local $count3 = {$count}
466+
.match {$count <anything1>}{$count3 <anything2>}
467+
```
468+
Notes:
469+
- The number of times the same variable is used twice in a match (or the older Select) is vanishingly small. Since it is an error — and the advice to fix is easy — that will prevent misbehavior.
470+
- There would be no change to the ABNF; but there would be an additional constraint in the spec, and relaxation of immutability within the .match statement.
471+
472+
**Pros**
473+
- No new syntax is required
474+
- Preserves immutability before and after the .match statement
475+
- Avoids the serious problem of mismatch of selector and format of option "Do Nothing"
476+
- Avoids the extra syntax of option "Allow both local and input declarative selectors with immutability"
477+
- Avoids the problem of multiple variables in "Allow immutable input declarative selectors"
478+
- Is much more consise than "Match on variables instead of expressions", since it doesn't require a .local or .input for every variable with options
479+
- Avoids the readability issues with "Provide a #-like Feature"
480+
481+
**Cons**
482+
- Complexity: `.match` means more than one thing
483+
- Complexity: `.match` implicitly creates a new lexical scope
484+
- Violates immutability that we've established everywhere else
485+
- Requires additional `.local` declarations in cases where a variable would occur twice
486+
such as `.match {$date :date option=monthOnly} {$date :date option=full}`
487+
488+

0 commit comments

Comments
 (0)