hi there,

I was playing a bit w/ pointer methods[1] to see how this could be applied to a 
pet peeve of mine: ensuring at compile-time that, e.g., json.Decoder.Decode 
takes a pointer to a value:

https://go2goplay.golang.org/p/J2Lr5QrkKTj

this works:

func Decode(type *T)(ptr *T) error {
        return json.NewDecoder(new(bytes.Buffer)).Decode(ptr)
}

func main() {
        var v int
        Decode(int)(&v)
}

as well as this:

type Decoder(type *T) struct{}

func (dec *Decoder(T)) Decode(ptr *T) error {
        // ...
        return nil
}

func main() {
        var v int
        dec := Decoder(int){}
        dec.Decode(&v)
}

but I was initially caught off guard trying to write the method on Decoder like 
so:

// ERROR:  receiver type parameter *T must be an identifier
func (dec *Decoder(*T)) Decode(ptr *T) error {
    return nil
}

(notice the extra '*' in Decoder(*T))

perhaps something to mention in the document?

-s

[1]: 
https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#pointer-methods



-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/GWEKljiXMj6tKH93oLqmg7OhW-WxDjjGG7fWY3p4KM6W-aCxZsLsEyFP9juc5o8mfIdPqM28NSJN-6anr85Teel-E8zaBdNwKE1_622WR5k%3D%40sbinet.org.

Reply via email to