Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial: Define a structured signature for PluralRuleSelect #594

Merged
merged 7 commits into from
Jan 6, 2022

Conversation

gibson042
Copy link
Contributor

Fixes #593

@gibson042 gibson042 requested a review from sffc July 15, 2021 19:50
@@ -374,7 +374,7 @@ <h1>FormatDateTimePattern ( _dateTimeFormat_, _patternParts_, _x_, _rangeFormatO
1. Else if _p_ is equal to *"timeZoneName"*, then
1. Let _f_ be _dateTimeFormat_.[[TimeZoneName]].
1. Let _v_ be _dateTimeFormat_.[[TimeZone]].
1. Let _fv_ be a String value representing _v_ in the form given by _f_; the String value depends upon the implementation and the effective locale. The String value may also depend on the value of the [[InDST]] field of _tm_. If the implementation does not have a localized representation of _f_, then use the String value of _v_ itself.
1. Let _fv_ be a String value representing _v_ in the form given by _f_; the String value depends upon the implementation and the effective locale. The String value may also depend on the value of the [[InDST]] field of _tm_. If the implementation does not have a localized representation of _f_, then use the String value of _v_ itself.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removal of consecutive whitespace resolves a warning from ecmarkup.

Comment on lines 105 to 119
<emu-clause id="sec-pluralruleselect" type="implementation-defined abstract operation">
<h1>
PluralRuleSelect (
_locale_: a String,
_type_: a String,
_n_: a finite Number,
_operands_: a Plural Rules Operands Record corresponding with _n_,
)
</h1>
<dl class="header">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Further adoption of structured operation headers can be accomplished later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is a new convention from ECMA-262? It looks great, thanks!

</h1>
<dl class="header">
<dt>description</dt>
<dd>It returns the String from &laquo; *"zero"*, *"one"*, *"two"*, *"few"*, *"many"*, *"other"* &raquo; that best categorizes _n_ according to the rules for _locale_ and _type_.</dd>
Copy link
Contributor Author

@gibson042 gibson042 Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that operands doesn't actually seem relevant to the signature of PluralRuleSelect... I'd like to take this PR even further, removing locale, type, and operands and replacing them with a single pluralRules argument (an instance with an [[InitializedPluralRules]] slot) for better comprehensibility and alignment with other parts of the spec, in the process also removing GetOperands as an implementation detail (since its only use is to construct operands).

Copy link
Contributor

@sffc sffc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Once this is merged I'll pull it in to NFv3.

Copy link
Member

@ryzokuken ryzokuken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Not sure if the follow-on change you mentioned will keep this PR editorial or not... @sffc @zbraniecki what do you think?

<p>
When the PluralRuleSelect abstract operation is called with four arguments, it performs an implementation-dependent algorithm to map _n_ to the appropriate plural representation of the Plural Rules Operands Record _operands_ by selecting the rules denoted by _type_ for the corresponding _locale_, or the String value *"other"*.
</p>
<emu-clause id="sec-pluralruleselect" type="implementation-defined abstract operation">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aoid is gone, I suppose automagic linking of this AO from algorithms won't work anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, this is great. I should really keep up with all the exciting new stuff in ecmarkup

Comment on lines 105 to 119
<emu-clause id="sec-pluralruleselect" type="implementation-defined abstract operation">
<h1>
PluralRuleSelect (
_locale_: a String,
_type_: a String,
_n_: a finite Number,
_operands_: a Plural Rules Operands Record corresponding with _n_,
)
</h1>
<dl class="header">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is a new convention from ECMA-262? It looks great, thanks!

@ryzokuken ryzokuken changed the title Define a structured signature for PluralRuleSelect Editorial: Define a structured signature for PluralRuleSelect Jul 22, 2021
@ryzokuken ryzokuken added the editorial Involves an editorial fix label Jul 22, 2021
@gibson042
Copy link
Contributor Author

gibson042 commented Jul 22, 2021

Not sure if the follow-on change you mentioned [removing locale, type, and operands and replacing them with a single pluralRules argument (an instance with an [[InitializedPluralRules]] slot) for better comprehensibility and alignment with other parts of the spec, in the process also removing GetOperands as an implementation detail] will keep this PR editorial or not... @sffc @zbraniecki what do you think?

I believe it would because PluralRuleSelect is called only from ResolvePlural, which pulls locale and type directly from slots on pluralRules and determines operands by invoking GetOperands on a value from the response of FormatNumericToString(pluralRules, n). So if PluralRuleSelect were provided with pluralRules and n similar to e.g. FormatDateTime(dateTimeFormat, x) and FormatNumeric(numberFormat, x), then it could do those steps itself (reading [[Locale]] and [[Type]] and determining how they affect the result of "zero" vs. "one" vs. "two" vs. "few" vs. "many" vs. "other").

Unless there's some reason why the internal behavior of plural rule selection MUST format the number to a numeric string and then use the resulting integer/fraction digit counts to make its determination, even though that determination is itself implementation-defined?

@anba
Copy link
Contributor

anba commented Jul 22, 2021

The number is formatted as a string, because plural rules make a difference between integers and non-integers:

js> new Intl.PluralRules("en", {minimumFractionDigits: 0}).select(1) 
"one"
js> new Intl.PluralRules("en", {minimumFractionDigits: 2}).select(1) 
"other"

And by specifying that FormatNumericToString and GetOperands must be used, we can control how the input number is formatted, which gives less wiggle room for an implementation to do the wrong thing.

@gibson042
Copy link
Contributor Author

gibson042 commented Jul 22, 2021

I just don't see how that's the case... non-normative references to CLDR and in-spec examples would help much more than requiring a roundabout trip from Number to String to Record—especially when the Record appears to be an attempt to recover integer/fraction/significant digits configuration that already exists on pluralRules. The current algorithm seems overly prescriptive, and rather divergent from other parts of the spec.

Maybe what's putting me off is that PluralRuleSelect has access to n in addition to the Record derived from inspecting its string serialization subject to pluralRules, but does not have access to pluralRules itself... it seems so strange to provide both n and operands.

@@ -79,35 +75,42 @@ <h1>GetOperands ( _s_ )</h1>
<td>Number of digits of [[Number]].</td>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sffc there are problems with the definitions of these fields:

  • [[IntegerDigits]] is defined as "Number of digits of [[Number]]", but is populated with the number value of the integer digits (e.g., it is equal to 8 for an input string like "8.540").
  • [[FractionDigits]] and [[NumberOfFractionDigits]] have effectively the same definition, even though the former is populated with the number value of the fractional digits while the latter is populated with the count of such digits (e.g., they are respectively equal to 540 and 3 for an input string like "8.540").
  • [[FractionDigitsWithoutTrailing]] and [[NumberOfFractionDigitsWithoutTrailing]] also have effectively the same definition, even though the former is populated with the number value of the truncated fractional digits while the latter is populated with the count of such digits (e.g., they are respectively equal to 54 and 2 for an input string like "8.540").

I'd like to fix this... do you know what the actual intent is?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are supposed to correspond to: https://unicode.org/reports/tr35/tr35-numbers.html#Operands

  • i | integer digits of n.
  • v | number of visible fraction digits in n, with trailing zeros.*
  • w | number of visible fraction digits in n, without trailing zeros.*
  • f | visible fraction digits in n, with trailing zeros.*
  • t | visible fraction digits in n, without trailing zeros.*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I added references and updated the text to match (which is still just editorial work).

@gibson042
Copy link
Contributor Author

gibson042 commented Jul 22, 2021

Maybe what's putting me off is that PluralRuleSelect has access to n in addition to the Record derived from inspecting its string serialization subject to pluralRules, but does not have access to pluralRules itself... it seems so strange to provide both n and operands.

Concrete proposal: drop GetOperands and change the signature of PluralRuleSelect to accept a String ([[FormattedString]] output from FormatNumericToString) rather than a Number and a Record. The string parsing of GetOperands would become an implementation detail, but the implementation-defined behavior would not have access to the raw number.

         1. Let _res_ be ! FormatNumericToString(_pluralRules_, _n_).
-        1. Let _s_ be _res_.[[FormattedString]].
-        1. Let _operands_ be ! GetOperands(_s_).
-        1. Return ! PluralRuleSelect(_locale_, _type_, _n_, _operands_).
+        1. Return ! PluralRuleSelect(_locale_, _type_, _res_.[[FormattedString]]).

@gibson042 gibson042 force-pushed the gh-593-improve-PluralRuleSelect branch 3 times, most recently from 299e92c to a93d608 Compare July 23, 2021 02:58
@gibson042 gibson042 force-pushed the gh-593-improve-PluralRuleSelect branch from a93d608 to b8993d2 Compare July 23, 2021 03:24
@gibson042 gibson042 mentioned this pull request Aug 15, 2021
spec/pluralrules.html Outdated Show resolved Hide resolved
@gibson042 gibson042 merged commit 06cfbb9 into tc39:master Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editorial Involves an editorial fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Be more clear on valid return values for PluralRuleSelect
5 participants