[whatwg/webidl] Type recursion (Issue #1078)

This question began over at [WebAssembly interface types](https://github.com/WebAssembly/interface-types/issues/137#issuecomment-994243162), where I noticed the claim that “WebIDL which does not support recursive types.” I was surprised by that because I am fairly certain that it does (as currently specified) — but at the same time, I’m not actually certain that it is _meant to,_ as I’m unaware of any APIs currently leveraging it.

Specifically, it appears that Web IDL currently permits recursion via most parametric types in some form or another, e.g.

```webidl
typedef sequence<(DOMString or StringTree)> StringTree;

// Or “intertype”, e.g.:

typedef sequence<(DOMString or Bar)> Foo;
typedef record<DOMString, Foo> Bar;
```

The spec appears to [forbid tautological typedefs](https://webidl.spec.whatwg.org/#:~:text=The%20Type%20must%20not%20be%20the%20identifier%20of%20the%20same%20or%20another%20typedef.) (e.g. `typedef Foo Foo;`). It also forbids dictionaries from including themselves via parametric types per [“includes a dictionary D”](https://webidl.spec.whatwg.org/#:~:text=includes%20a%20dictionary%20D). The latter was cited as one of the reasons it had been concluded that Web IDL types do not permit recursion, but this constraint is unique to dictionaries.

I was never certain of the reason for the dictionary constraint apart from a suspicion that it related to default-dictionaries, which is somewhat supported by the context for [this PR](https://github.com/whatwg/webidl/issues/296), though it’s still not clear to me why this would be problematic in the context of sequence or record types. Sequence conversion in particular could be infinite with or without these constraints.

The one clue that I can find suggesting an intent that recursion not be permitted is that [“type names”](https://webidl.spec.whatwg.org/#dfn-type-name) for the parametric types that would otherwise appear to permit recursion do not account for it. While this does suggest the intent, though, the type names are sorta just hypothetical, so I’m not sure it counts for much that some would be infinitely long.

So — is all recursion meant to be prohibited? If so, is there actually anything in the spec prohibiting it? And if not, are there any APIs known to leverage it?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/webidl/issues/1078

You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/webidl/issues/1078@github.com>

Received on Saturday, 18 December 2021 07:52:35 UTC