whatwg / webidl

Web IDL Standard
https://webidl.spec.whatwg.org/
Other
403 stars 161 forks source link

Allow dictionary values to default to undefined #983

Open domenic opened 3 years ago

domenic commented 3 years ago

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 commented 3 years ago

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

petervanderbeken commented 2 years ago

I think this was also fixed by https://github.com/whatwg/webidl/pull/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 commented 2 years ago

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 commented 2 years ago

https://github.com/plinss/widlparser does not seem to accept this syntax so I cannot use it in https://github.com/whatwg/html/pull/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.)