**please use this as single type parameter version instead: https://gotipplay.golang.org/p/ZrOqCj1R_va
On Tuesday, February 1, 2022 at 11:29:05 PM UTC-5 Ed Pelc wrote: > Hello, > > I'm trying out the go1.18beta2 by attempting to move from codegen based > generics using a custom tool to 1.18's type parameters. I looked > extensively for examples and others trying to do something similar but > found little. I figured out a working way to do this but it seems extensive. > > I'm trying to have a generic field on a struct(UpdateModelRequest) which > is a *T so that the struct can be read from json and then processed. A few > methods or fields need to be exposed so that you can interact with the > generic field from UpdateModelRequest's methods. I also need to be able to > check if this generic field is left nil. > > I was able to get this to work but I'm curious if it could be simplified > at all as it seems complicated to require two type parameters for this. I > figured this out after trying to follow the `pointer method` > <https://go.googlesource.com/proposal/+/8b5daebee3198c7b6265b40c06b077be5e4c5b4b/design/43651-type-parameters.md> > section > from the generics proposal. It seems to be written for a slightly different > use case though. > > Working, but complicated(requires two type params): > https://gotipplay.golang.org/p/iz2GKYtaL-A > > Basically I do not understand why you can't do a simpler single type > parameter version using comparable directly within DataRecord. I know the > spec says comparable cannot be used as a variable, or value(and I presume > this means a struct field too) but it works with the two type parameter > redirection above. I'd like to know why the two parameter version works for > some reason. > > Single type parameter version: https://gotipplay.golang.org/p/UO8e6zQwi0k > > > My research/help others find this. I could not find much for people trying > to do something similar even though it seems a common use case(we have many > different data types that need basic crud http handlers): > > From reading the 1.18 spec it says you can't embed `comparable` types as a > variable or value. I assume this means you also can't use as a struct > field. I'm not sure why you can't do this or if it's just a limitation for > now. Are you expected to add an `IsNil() bool` method as a replacement for > this? > > From the spec re comparable type parameter(Record field in example) as a > struct field: > https://tip.golang.org/ref/spec#Type_constraints > > "They cannot be the types of values or variables, or components of other, > non-interface types." > > If you try to use a pointer field(line 12) to make the record comparable > then you get this error. > > "req.Record.SetID undefined (type *Record is pointer to type parameter, > not type parameter)" > -- 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/55922900-431e-4c05-9e79-808754c9919fn%40googlegroups.com.