Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect: StructOf doesn't support recursive structs #20013

Open
ben-clayton opened this issue Apr 17, 2017 · 5 comments
Open

reflect: StructOf doesn't support recursive structs #20013

ben-clayton opened this issue Apr 17, 2017 · 5 comments

Comments

@ben-clayton
Copy link

@ben-clayton ben-clayton commented Apr 17, 2017

go version: 1.8

I've been playing with StructOf to build go values that match a serialized schema-driven form.
I've encountered an edge case that I'm uncertain whether Go currently supports. Given the struct:

type S struct {
   Ptr *S
}

I see no way to forward-declare the S struct to use it as a pointer field to StructOf.

Is there some way I can build this struct type?

@bradfitz bradfitz changed the title StructOf pointer field to itself reflect: StructOf doesn't support recursive structs Apr 17, 2017
@bradfitz bradfitz added this to the Unplanned milestone Apr 17, 2017
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Apr 17, 2017

/cc @crawshaw

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 17, 2017

You can't do it. This is essentially a dup of #16522. Since you can't build a named type with reflect, you can't build a self-referential type.

@jimmyfrasche
Copy link
Member

@jimmyfrasche jimmyfrasche commented Aug 22, 2017

@ianlancetaylor I don't see how this is essentially a dupe, though it is certainly related (and blocked on) #16522.

With #16522 you could do

s := reflect.StructOf(someFields) //want this to contain n but n doesn't exist yet
n := reflect.NamedOf(s, "S", nil)

but without a mechanism for forward declaration, as mentioned in the OP, how do you add a field to s of type n when the construction of n requires that s was already constructed?

(A related concern came up in #4146 where you would need n to define the signatures of the methods to add to n, but that's easier to handle).

@dockercore
Copy link

@dockercore dockercore commented May 2, 2018

s := reflect.StructOf(someFields) //want this to contain n but n doesn't exist yet
n := reflect.NamedOf(s, "S", nil)

@cosmos72
Copy link

@cosmos72 cosmos72 commented Jun 13, 2020

If accepted and implemented, proposal #39528 would solve this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants