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` section from the generics proposal. It seems to be written for a slightly different use case though.
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.
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:
"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)"