Where is defined what recursive type declarations are allowed?

95 views
Skip to first unread message

Axel Wagner

unread,
May 3, 2021, 7:40:02 PM5/3/21
to golang-nuts
Hi,

this thread had me wonder again about a spec question that recently came up which I didn't have an answer to: Where does the spec say, which recursive type declarations are allowed and which aren't? For example, why is `type T *T` legal, but `type T T`¹ isn't?

To be clear: I know why we can't make `type T T` et al. legal - the compiler needs to know the size of the values and it can't know it from a declaration like `type T T`. I'm wondering how this follows from the spec.

I haven't read through all of the spec in a while, so I might just be missing an obvious section. But it doesn't seem to be described in the section about type declarations and some obvious keyword searches like "recursive" or "size" didn't turn up anything meaningful, as far as I can tell. But feel free to point me to a section I overlooked, and apologies if the question makes me seem lazy because it ends up being obvious :)

[1] Maybe `type T struct { T }` is a better example, as it's a type-literal, not a type name - just like `type T *T`.

Ian Lance Taylor

unread,
May 3, 2021, 7:47:26 PM5/3/21
to Axel Wagner, golang-nuts, Robert Griesemer
I think you are correct that this is not explicitly defined anywhere.
The basic rule is fairly straightforward: a type definition can't
contain or embed an instance of itself, but it can otherwise refer to
itself. But that ought to be in the spec, and as far as I know it is
not.

Ian

Axel Wagner

unread,
May 4, 2021, 2:04:03 AM5/4/21
to Ian Lance Taylor, golang-nuts, Robert Griesemer
Thanks :) At least I didn't overlook anything :)

Jesper Louis Andersen

unread,
May 4, 2021, 10:36:33 AM5/4/21
to Axel Wagner, Ian Lance Taylor, golang-nuts, Robert Griesemer
On Tue, May 4, 2021 at 8:03 AM 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:
Thanks :) At least I didn't overlook anything :)

fwiw, I also searched the spec and found nothing. I found that peculiar. Recursive types require special treatment in type theory, so it is likely any language implementation which supports the notion is bound to grapple with the static semantics of recursive types at some point. Not doing so risk implementations to subtly differ.

--
J.
Reply all
Reply to author
Forward
0 new messages