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

Should all new constructors throw when called as a function? #384

Closed
gibson042 opened this issue Nov 17, 2019 · 5 comments · Fixed by #491
Closed

Should all new constructors throw when called as a function? #384

gibson042 opened this issue Nov 17, 2019 · 5 comments · Fixed by #491
Labels
c: meta Component: intl-wide issues editorial Involves an editorial fix s: discuss Status: TG2 must discuss to move forward
Milestone

Comments

@gibson042
Copy link
Contributor

Every constructor except PluralRules is specified to be callable as a function and supported as such:

$ eshost -se 'typeof Intl === "undefined" ?
  "Intl: undefined" : 
  "Collator,NumberFormat,DateTimeFormat,PluralRules".split(",").map(name => {
    let src = `Intl.${name}()`, result;
    try {
      result = eval(src).constructor.name;
    } catch ( err ) {
      result = err.name;
    }
    return `${src}: ${JSON.stringify(result)}`;
  }).join("\n")'
#### Chakra, JavaScriptCore, SpiderMonkey, V8
Intl.Collator(): "Collator"
Intl.NumberFormat(): "NumberFormat"
Intl.DateTimeFormat(): "DateTimeFormat"
Intl.PluralRules(): "TypeError"

#### XS
Intl: undefined

PluralRules is intentionally different per tc39/proposal-intl-plural-rules#24 , but the door was left open to bring it in line with the rest of spec. Can we reach consensus on whether or not the behavior of Collator, NumberFormat, and DateTimeFormat when called as a function is to be treated as the ECMA 402 convention or as an abandoned legacy, and document accordingly in https://tc39.es/ecma402/#locale-and-parameter-negotiation ?

@ljharb
Copy link
Member

ljharb commented Nov 18, 2019

It seems better to make them behave like the class keyword, otherwise they won’t be ergonomically self hostable in JS by implementations.

@gibson042
Copy link
Contributor Author

Well, that's already the case for Intl.{Collator,NumberFormat,DateTimeFormat}, so no matter what such implementations will require code similar to export const NumberFormat = makeCallableFromClass(NumberFormatClass, {name: "NumberFormat"}). This is just a question of how uniformly such code would have to be applied, and also guidance to keep the spec itself predictable through future changes.

@sffc sffc added c: meta Component: intl-wide issues s: discuss Status: TG2 must discuss to move forward labels Nov 18, 2019
@anba
Copy link
Contributor

anba commented Nov 20, 2019

General consensus (in ECMA-402 and ECMA-262) is that any new built-in constructor should require new. Only pre-ES6 constructors (such as Intl.{Collator, NumberFormat, DateTImeFormat}) support a legacy mode which allows to call them without new.

@sffc sffc added this to the ES 2021 milestone Jun 5, 2020
@sffc sffc added the editorial Involves an editorial fix label Jun 5, 2020
@sffc
Copy link
Contributor

sffc commented Jul 8, 2020

Is there any action required on this issue? Do we need to document the decision somewhere? Or can we just close it?

@leobalter
Copy link
Member

I believe it's fine to close this, we can discuss (IMO directly in a new PR) if we want to add a note to the specs about this, like in #463.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: meta Component: intl-wide issues editorial Involves an editorial fix s: discuss Status: TG2 must discuss to move forward
Projects
Status: Other Issues
Development

Successfully merging a pull request may close this issue.

5 participants