message-format-wg icon indicating copy to clipboard operation
message-format-wg copied to clipboard

Spec clarification: selection without a function

Open mihnita opened this issue 3 years ago • 4 comments

The current spec allows this:

match {$foo}
   when 1 {...}
   when one {...}
   when male {...}
   when foggy {...}
   when * {...}

I would argue that this is both useless and dangerous.

First the utility. As a translator if I see match {$foo} * {...} I have absolutely no idea what kind of options I can add. Gender options? Plural options? Something else?

Danger This can't be linted, as the type is not available. And at runtime it might have unexpected side effects, especially in some languages. Imagine a plural where the $bar becomes a string by mistake. All of a sudden 1 does not match anymore.

mihnita avatar Aug 17 '22 22:08 mihnita

I'm comfortable with removing support for it from the spec - it's a backward compatible extension and we can add it if we see a use case later on.

zbraniecki avatar Aug 17 '22 22:08 zbraniecki

What if $foo here was defined by a local variable, such that its type was available? That could well make sense if the same value was being used both for selection and formatting.

To me, this sounds really rather similar to the question of whether values within a pattern without a formatting function should be allowed or not, such as {The {$foo} thing}. Having the rules be different for selectors and pattern values seems weird.

I would strongly prefer keeping unadorned selectors (and pattern values) allowed in the syntax and spec, but to leave it to each implementation to determine what to do with a function-less variable reference. This would allow each implementation to figure out whether the value's type is supported and handle it with one of its known formatters or matchers, or to complain about a type error.

eemeli avatar Aug 18 '22 09:08 eemeli

Having the rules be different for selectors and pattern values seems weird.

I don't think I agree. Having them separate feels acceptable to me and tightening one (in a way that can be later relaxed) doesn't feel wrong.

zbraniecki avatar Aug 18 '22 13:08 zbraniecki

I think once we start discussing what runtime types we recognize in MF2, we'll also come back to this question for scenario like Fluent's Partially Formatted Variables:

let source = `{
  match {$floor}
     when 1 {...}
     when * {...}
}`;
let mf = new Intl.MessageFormat();
mf.formatToString(source, {
  floor: Intl.MF2.PluralRule(floor, type: "ordinal")
});

zbraniecki avatar Aug 18 '22 13:08 zbraniecki

Selectors now require an annotation. Reopen if needed.

aphillips avatar Sep 04 '23 21:09 aphillips