whatwg / webidl

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

Consider disallowing optional dictionary arguments that have required members #793

Open bzbarsky opened 5 years ago

bzbarsky commented 5 years ago

Consider this IDL:

dictionary X {
  required boolean myVal;
};

interface Foo {
  void bar(optional X arg);
};

This is currently allowed by the spec. But in terms of behavior, what does the implementation of bar do if the dictionary is not passed? Specifically, what does it use in the place where it was going to use myVal? I can see a few options:

1) It treats it effectively as a tristate, with no value if the dictionary was not passed. But then it could just make it optional in the dictionary too.

2) It assumes a value for myVal (true or false). Then it could just make it optional in the dictionary and give it a default value.

Either way, it doesn't make sense to have this dictionary member be required in this situation. Hence I would propose that we disallow optional arguments whose type is a dictionary with required members. It might still make sense to have optional dictionary members whose type is a dictionary that has required members, I guess...

@domenic @annevk @saschanaz @Ms2ger thoughts?

bzbarsky commented 5 years ago

I should note that there are no cases like the one I am proposing disallowing in the parts of the web platform Gecko implements right now, and in fact Gecko's code generator fails out in that situation. That is not what prompted me to file this issue; @foolip's comments in https://github.com/w3c/webidl2.js/issues/407 did, but it's a data point in terms of whether people would be affected in practice.

Ms2ger commented 5 years ago

Hence I would propose that we disallow optional arguments whose type is a dictionary with required members.

Makes sense to me.

It might still make sense to have optional dictionary members whose type is a dictionary that has required members, I guess...

I think we might as well disallow it.

bzbarsky commented 5 years ago

I think we might as well disallow it.

See https://github.com/heycam/webidl/issues/76#issuecomment-393899041 -- people are using that pattern in specs...

saschanaz commented 4 years ago

people are using that pattern in specs...

Only for dictionary members, right? I see no special value to allow it for arguments, where things like https://github.com/whatwg/html/issues/5064 is just broken and not really optional.

BTW, do you recall which specs use that pattern?

bzbarsky commented 4 years ago

BTW, do you recall which specs use that pattern?

https://w3c.github.io/media-capabilities/#media-configurations is an example.