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 · 4 comments

Comments

@ben-clayton
Copy link

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

This comment has been minimized.

Copy link
Member

commented Apr 17, 2017

/cc @crawshaw

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.