IssueRaiser

proposal: spec: support for struct members in interface/constraint syntax

### Author background - **Would you consider yourself a novice, intermediate, or experienced Go programmer?** I consider myself an intermediate to experienced go programmer writing Go for the last 4 years and professionally for the last 2 years. - **What other languages do you have experience with?** I have experience in JS/TS, and a tiny bit of rust but nothing to bat an eye at. ### Related proposals - **Is this about generics?** yes ### Proposal - **What is the proposed change?** The proposal is to extend the interface/constraint syntax to support struct members. - **Who does this proposal help, and why?** This proposal would allow for generic functions to be structurally generic. It would reduce a lot of boilerplate around getters and setters for simple struct members, and would/might/could increase performance for generic code by avoiding dynamic dispatch of interface methods for simple property accesses. - **Please describe as precisely as possible the change to the language.** As a little bit of background, at the time of this proposal there is currently an [issue](https://github.com/golang/go/issues/48522) being tracked for go1.19 about being able to use common struct members for generic types that are the union of structs . For example: ```go type Point2D struct { X, Y float64 } type Point3D struct { X, Y, Z float64 } func SomeOperation2D[T Point2D | Point3D](point T) { return point.X * point.Y } ``` This fails like so: ``` point.X undefined (type T

Posted by davidmdm on Feb 18, 2022, 5:14 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

support (Software)

proposal: spec: support for struct members in interface/constraint syntax

davidmdmFeb 18, 2022, 5:14 PM

### Author background - **Would you consider yourself a novice, intermediate, or experienced Go programmer?** I consider myself an intermediate to experienced go programmer writing Go for the last 4 years and professionally for the last 2 years. - **What other languages do you have experience with?** I have experience in JS/TS, and a tiny bit of rust but nothing to bat an eye at. ### Related proposals - **Is this about generics?** yes ### Proposal - **What is the proposed change?** The proposal is to extend the interface/constraint syntax to support struct members. - **Who does this proposal help, and why?** This proposal would allow for generic functions to be structurally generic. It would reduce a lot of boilerplate around getters and setters for simple struct members, and would/might/could increase performance for generic code by avoiding dynamic dispatch of interface methods for simple property accesses. - **Please describe as precisely as possible the change to the language.** As a little bit of background, at the time of this proposal there is currently an [issue](https://github.com/golang/go/issues/48522) being tracked for go1.19 about being able to use common struct members for generic types that are the union of structs . For example: ```go type Point2D struct { X, Y float64 } type Point3D struct { X, Y, Z float64 } func SomeOperation2D[T Point2D | Point3D](point T) { return point.X * point.Y } ``` This fails like so: ``` point.X undefined (type T

Comments (0)

No comments yet. Start the discussion.

Comment on this issue