Refactor to use generics to have a single Make

145 views
Skip to first unread message

sbezverk

unread,
May 3, 2022, 3:06:46 PM5/3/22
to golan...@googlegroups.com

Hello,

 

I am starting to learn generics and I was wondering if the following code could be refactored to use genercis, in order to avoid using per type Make() method.

 

https://go.dev/play/p/gE4Z6Zj19Is

 

I have 3 identical structs, they are defined with different types and used in other types by their unique types. The function to create all 3 types can be consolidated as the actual structure of is the same.

 

I am curious it would be possible to leverage generics for this. I have made an attempt but I still need to use per type Make method.

 

Thank you and appreciate a lot your help.

 

Serguei

Ian Lance Taylor

unread,
May 3, 2022, 5:17:08 PM5/3/22
to sbezverk, golan...@googlegroups.com
There is no simple way to do this at present. This is related to
https://go.dev/issue/48522 and https://go.dev/issue/51259.

Ian

Sean Liao

unread,
May 3, 2022, 7:01:48 PM5/3/22
to golang-nuts
right now it only works if the structs are actually identical:

- sean


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/D41C14EF-35D1-434F-A719-1E8C88D90C0F%40gmail.com.

tapi...@gmail.com

unread,
May 3, 2022, 10:10:17 PM5/3/22
to golang-nuts
On Wednesday, May 4, 2022 at 7:01:48 AM UTC+8 se...@liao.dev wrote:
right now it only works if the structs are actually identical:

- sean


Though accessing fields is still not allowed.
Reply all
Reply to author
Forward
0 new messages