Sorry, you are right, inheritance is a better term. As written in the test, I unmarshalled the json array to a slice of different types boxed to the base structure (so I can safely use all the base fields in slice elements) and I can safely cast elements to specialized struct (in which I can safely use specialized fields as well as the base ones).
This is a common use case for me - I need to work with data structured like that and that is how I model the issue. At the moment I write specific boxing struct and boxing interface for each inheritance modeling (I don't know of a better way of modeling this kind of data in golang). With generics, boxing struct / interface can be reused - which greatly simplifies the code. But again I guess I am not so specific and that this is a common problem that many gophers face. The point of my feedback is that I found generics useful for inheritance implementation in go, which was not obvious to me, so I present you with that idea (as examples in the generics draft were very interesting to me and I think this one is as well). Deeper feedback is that it might be possible to introduce some clever language syntax that enable inheritance in go (or something close to inheritance) with using generics behind the curtains. On Sat, Jul 4, 2020 at 9:58 AM roger peppe <rogpe...@gmail.com> wrote: > When you say "polymorphism", I think you might mean "inheritance"? > (Go-with-generics already has two kinds of polymorphism) > > I don't really understand what you're trying to accomplish here. Perhaps > you could explain your motivation a bit more? > > > On Fri, 3 Jul 2020, 21:22 Aleksandar Milovanović, < > aleksan...@videobolt.net> wrote: > >> If someone is interested I simplified the code, commented some more and >> added the unmarshall json in the example - >> https://go2goplay.golang.org/p/I_ezQ9wyWVx. >> >> Still, I don't know how to type the BuildEmptySpec() in BoxBaseI better >> than to return a simple interface{}... If it would >> return BoxSpecI(BoxBaseI) it would not be enough. >> >> Aleksandar >> >> >> >> On Fri, Jul 3, 2020 at 1:54 PM Aleksandar Milovanović < >> aleksan...@videobolt.net> wrote: >> >>> Hi guys, >>> >>> Thanks for the effort, it is much needed for quite some time. I really >>> appreciate the way of introducing the changes, go2go tool and open >>> discussion, so I wanted to review them and participate with feedback so you >>> can do the best job of understanding the world. With proposed changes, I >>> think extending language to support something similar to polymorphism might >>> be cheap. >>> >>> Consider this go playground: https://go2goplay.golang.org/p/jqA4ccWeAKD. >>> >>> Syntax is awkward currently (some syntax sugar is definitively needed), >>> but the point in the test below stands - you do have polymorphic slice, you >>> can use base fields and methods, and you can cast to the child structure >>> (which contains all the fields and methods from the base). json marshaling >>> was added to the example, json unmarshal could be implemented similarly, >>> but in that case we need provided mapping from base structure to the >>> specialized one, which can't be typed with current generics (for some of >>> it, interface{} and reflect would be needed). Could provide that example as >>> well, if needed. >>> >>> Sending this idea as I think it might be interesting, and I think you >>> might get some more ideas out of it. >>> >>> Aleksandar >>> >> -- >> 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+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/CAG9NSAWtK%3DeUR9%2B_vCb0X5yun6VF3SB6WOkfq5%2B-cX7KXNQ9vg%40mail.gmail.com >> <https://groups.google.com/d/msgid/golang-nuts/CAG9NSAWtK%3DeUR9%2B_vCb0X5yun6VF3SB6WOkfq5%2B-cX7KXNQ9vg%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAG9NSAU-WTrC1LsN3XzLvzpt4wP%2BLh67DEHRb0NfXUuhzhzFUg%40mail.gmail.com.