webidl icon indicating copy to clipboard operation
webidl copied to clipboard

Detect whether a dictionary member was set by default or not

Open beaufortfrancois opened this issue 2 years ago • 2 comments

It would be nice if WebIDL would provide a way to know whether a dictionary member was set by default or not.

dictionary MyDictionary {
    required boolean myRequiredMember;
    boolean myOptionalMember = false;
};

In that case, myOptionalMember could be false because it was either set explicitly by developer with { myRequiredMember: true, myOptionalMember: false } or because it was not with { myRequiredMember: true }.

Having a way to detect this would allow implementations to warn developers that a dictionary was partially specified which may result in unexpected behaviour. For instance, { myRequiredMember: true, myOptionalMemberWithTypo: true } would set myOptionalMember to false.

Some would argue that if it's the case dictionary members should be declared required. I'm not sure about it. Providing default values is good but typos exist. I'd like to see efforts on that front.

beaufortfrancois avatar Apr 28 '23 08:04 beaufortfrancois

How is myOptionalMemberWithTypo different to a potential future member the implementation might not yet understand?

In any event, if an implementation wanted to warn for this they already can. They'd just implement the defaulting outside of IDL.

annevk avatar Apr 28 '23 11:04 annevk

It would be very surprising to me to receive a warning when making use of a feature that’s seemingly been deliberately designed to improve developer ergonomics. It also would seem arbitrary to single out default dictionary member values without also warning for default argument values; they are two realizations of the same concept.

bathos avatar Apr 30 '23 17:04 bathos