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
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest
Milestone

Comments

@ben-clayton
Copy link

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

/cc @crawshaw

@ianlancetaylor
Copy link
Contributor

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

@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

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

@cosmos72
Copy link

If accepted and implemented, proposal #39528 would solve this

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FeatureRequest
Projects
None yet
Development

No branches or pull requests

7 participants