On Mon, May 3, 2021 at 4:39 PM 'Axel Wagner' via golang-nuts <golang-nuts@googlegroups.com> wrote: > > 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`.
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 -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcUenYfoGC1HumLDYWdDaBqFo1pGkV2Wk6N%2Bx%3DaQyQNzxQ%40mail.gmail.com.