for completeness' sake, there is indeed no need for pointer methods:

https://go2goplay.golang.org/p/1WD7WRAmNim

(my main point was the gotcha w/ 'func (dec *Decoder(*T)) F()')




‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, June 17, 2020 9:08 AM, Sebastien Binet <d...@sbinet.org> wrote:

> 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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/roat7U2Kz3wJuH4iEOGB4UQWotb0JaWnTNC5-M0_TatW2Woy65ZVeBrVGM-Ux4kALyMivG3NK-RWqtYhWGresIY5Jq_If3oPfr_Arzu6Ju4%3D%40sbinet.org.

Reply via email to