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

Allow dictionary values to default to undefined #983

Open
domenic opened this issue May 20, 2021 · 4 comments
Open

Allow dictionary values to default to undefined #983

domenic opened this issue May 20, 2021 · 4 comments

Comments

@domenic
Copy link
Member

domenic commented May 20, 2021

I am writing spec text like this:

dictionary D {
  any data;
};

Let |data| be |dict|["data"] if it exists, or undefined otherwise.

It would be nicer if I could write this:

dictionary D {
  any data = undefined;
};

Let |data| be |dict|["data"].

The spec currently does talk about undefined constant values. But I think that's separate from the values that are allowed as dictionary defaults, which are specified in https://heycam.github.io/webidl/#idl-dictionaries in the section starting

If the identifier is followed by a U+003D EQUALS SIGN ("=") and a value (matching DefaultValue), then that gives the dictionary member its default value.

It looks like dictionary defaults are specified in terms of optional argument defaults, which are specified in terms of constants. So the most natural way to make this work is to thread through discussion of undefined constants there, plus grammar updates. We'd have to either say that constant types can be "primitive types or undefined", or we'd have to add undefined to the list of primitive types. Hmm...

@annevk
Copy link
Member

annevk commented May 21, 2021

If we attempted to tackle #905 this would fall out of that, no?

@petervanderbeken
Copy link

petervanderbeken commented Jun 3, 2022

I think this was also fixed by #1140

DefaultValue now includes undefined, and the optional argument section has

When the undefined token is used as the default value, the value is the IDL undefined value.

The conversion code from ES value to WebIDL dictionary will just function correctly in step 4.4.[1-2] at https://webidl.spec.whatwg.org/#es-dictionary

@petervanderbeken
Copy link

Although we might have missed adding undefined to

When a boolean literal token (true or false), the null token, an integer token, a decimal token, one of the three special floating point literal values (Infinity, -Infinity or NaN), a string token, the two token sequence [], or the two token sequence {} is used as the default value, it is interpreted in the same way as for an operation's optional argument default value.

under https://webidl.spec.whatwg.org/#ref-for-dfn-dictionary-member-default-value%E2%91%A4

@annevk
Copy link
Member

annevk commented Jun 7, 2022

https://github.com/plinss/widlparser does not seem to accept this syntax so I cannot use it in whatwg/html#7983. If the above issue gets fixed in Web IDL let's file an issue against widlparser and then afterwards we can simplify the HTML Standard a little bit for the ErrorEvent class.

(It also seems a little weird to both have = undefined and members implicitly being optional (which also yields undefined if they don't have an explicit default), but I suppose that transitional situation is okay until someone gets to #905.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants