On Wed, Feb 2, 2022 at 5:29 AM Ed Pelc <edrock...@gmail.com> wrote:

> 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
>

This seems to be what you are supposed to do, yes. Note that the caller
doesn't need to really concern themselves with the second type-parameter.
It's virtual, in a sense.

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
>

The compiler tells you why this doesn't work: You are comparing to `nil`
and not all values can be compared to `nil`. For that, you need the type
argument to be a pointer specifically. Telling the compiler that it is a
pointer, is what the second type parameter is for.

You can do this, if you want to: https://gotipplay.golang.org/p/aDGzOF7EWIF

But, of course, this means that your type might be instantiated with a
non-pointer and it's no longer possible to distinguish between "the zero
value" and "not given".


> 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/4b4ce168-64fd-4534-9925-989b1371a32bn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4b4ce168-64fd-4534-9925-989b1371a32bn%40googlegroups.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/CAEkBMfE%2BFvOqmPOE69LYo5B913uXesy%3DoG3Q9dUtz%3D%2BxBOmk%3DA%40mail.gmail.com.

Reply via email to