Repository navigation
Editorial: Add GetBooleanOption and GetStringOption - #3982
Conversation
|
The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/3982 and as multiple pages at https://tc39.es/ecma262/pr/3982/multipage . |
|
Small complication is that 402 |
gibson042
left a comment
There was a problem hiding this comment.
I'm annoyed by the repetition across Get${Type}Option operations as compared to a single GetOption operation, but not so much that I object to them.
Small complication is that 402
GetOptionthrowsRangeErrorfor invalid string values, so we'd either have to see if a normative change to do the same in 262 is web compatible (probably?) or make the optioncoerce/reject-typeerror/reject-rangeerror.
ECMA-262 functions throwing a TypeError for an invalid string option value where analogous ECMA-402 functions throw a RangeError seems like strictly worse behavior that I would love to correct via normative PR (I think I even commented on one of the former proposals, although likely too late to have made a difference), but failing that—or in advance of it—generalizing the parameter to support both specs seems good to me.
|
I'm not strictly against folding it back into a single |
|
A single one seems simpler to me, personally. |
|
Given tc39/proposal-joint-iteration#38 the normative change would have to target ECMA-402, it seems that the recent cases of this ( And yet Temporal uses |
|
Added two commits to support the ECMA-402 use cases ( |
5e6d8cf to
a82d5fd
Compare
gibson042
left a comment
There was a problem hiding this comment.
Note that Temporal GetRoundingIncrementOption is also a case for reading in integers, e.g.
1. Let _int_ be ? GetIntegerOption(_options_, *"roundingIncrement"*, ~coerce~, 1).
1. If _int_ is not in the inclusive interval from 1 to 10<sup>9</sup>, throw a *RangeError* exception.
1. Return _int_.
I'd like to say the same for ECMA-402 DefaultNumberOption, but that's further out because it checks MV bounds before snapping to integer (tc39/ecma402#691) and therefore in its current form would need e.g.
1. Let _mv_ be ? ValidateMVOption(_value_, ~coerce~, _fallback_).
1. If _mv_ is not in the inclusive interval from _minimum_ to _maximum_, throw a *RangeError* exception.
1. Return _mv_.
There's also GetTemporalFractionalSecondDigitsOption, but its type mixture probably precludes its use of these single-type patterns.
Whether generalization to integer and MV types supports pursuing the polymorphic {GetOption,ValidateOption} approach over a monomorphic {Get,Validate}${Type}Option is still debatable, but the latter is definitely friendlier to ESMeta and more supportive of type-specific parameters like allowedValues for strings and (minimum, maximum) for numerics, even if it does come with lots of repetitive boilerplate. But all in all, I wouldn't object to landing this PR as-is, or with something like ValidateStringOption to sweep in String.prototype.normalize as well.
I've done that in a second commit, works out nicely IMO. |
… and RangeError Ref tc39/ecma262#3982 (comment) An narrower alternative to tc39#176: SyntaxError for large sets defined by grammar/pattern/etc., RangeError only for numeric intervals.
… and RangeError Ref tc39/ecma262#3982 (comment) A narrower alternative to tc39#176: SyntaxError for large sets defined by grammar/pattern/etc., RangeError only for numeric intervals.
gibson042
left a comment
There was a problem hiding this comment.
As discussed in today's editor call, the defaultValue parameters should not be optional.
Option bags have historically been rare in ECMA-262, which is why so far only ECMA-402 provides a dedicated
GetOptionAO. Now thatGetOptionsObjecthas been pulled into 262 (#3655) and option bags are increasingly used to customize behavior of existing built-ins it seems like a good time to introduce helper AOs. Since normative conventions dictate different coercing behavior from the traditional status quoGetOptioncannot be reused directly and we must instead provide an option for behavior on type mismatch, additionally I think splitting into multiple typed variants increases readability.I decided against adding
GetObjectOptionas there is no coercion behavior and the name has an unfortunate similarity toGetOptionsObject; for now thatGetand throw logic remains inlined inIterator.zipfor itspaddingoption.Use cases outside of what's been changed here:
ECMA-402 can drop
GetOptionin favor of these new AOsTemporal heavily uses string options
GetBooleanOptionwas written with throw instead of coercing proposal-json-parseimmutable#21 in mind: