Re: [whatwg/webidl] Web IDL best practices for platform object construction/initialization (Issue #1060)

> but how do you communicate the "with its type attribute set to type" part?

You cannot set attributes to values. You can only define getter/setter steps for them. So you'd want to define an internal slot (either using the "[[Type]]" style or just the "has an associated type" style) and have the getter return that. Then, after newing up your object, you can set its internal slot value.

(Some older specs, or newer specs copying older specs, set attributes to values. This is bad and nonsensical. The only perhaps-reasonable exception is for Event subclasses, since those have a strange kind of autogenerated structure that isn't well-defined right now. I personally try to distinguish those cases by using the not-defined-anywhere magic word "Initialize" in the hopes of making it clear that we're doing something a bit different.)

Example: https://wicg.github.io/app-history/#navigate-event-destination defines a class, its internal slots, and its getter steps. https://wicg.github.io/app-history/#fire-a-push-or-replace-navigate-event creates one and modifies its internal slots before handing it off to other algorithms.

> Is one expected to go down the "define a 'initalize MyInterface with arg1 and arg2' algorithm" route

Yes.

In general it's not redundant because it's very rare that the constructor steps are exactly the same as the spec-creation steps. For example constructor steps often perform validation on the arguments. Or they take Web IDL types (e.g. USVString) instead of spec types (e.g. URL record).

What's left in common is generally only very simple things of the form "Set _x_'s y to yValue". And usually there are not very many of those. In my personal opinion, as a matter of editorial style, creating an algorithm which wraps those steps is obfuscating, i.e. I find "create a MyInterface with arg1, arg2, and arg3" less clear than spelling out "Let x be a new MyInterface / set x's slot1 to arg1 / set x's slot2 to arg2 / set x's slot3 to arg3". So unless you have many call sites in the spec, and many internal slots to initialize, the savings from such a centralized algorithm are not worth it IMO.

---

Hope this helps, and happy to take PRs or suggestions for how to better document this sort of thing. I guess a lot of it comes down to us still not having documented internal slots very well.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/webidl/issues/1060#issuecomment-961944754

Received on Friday, 5 November 2021 14:34:00 UTC