struct T, interface I. implement I for T and/or for *T

87 views
Skip to first unread message

andreas graeper

unread,
Aug 18, 2025, 12:26:06 PMAug 18
to golan...@googlegroups.com
type I interface { m()bool }
type T struct { x bool }
 func (o T) m()bool { return o.x }    // o:object
 func (p *T) m()bool { return p.x }   // p:pointer

both does not work. it is not possible to implement I for T and *T ( m already defined )

func fi(i I){}
func fp(i*I){}

fi(o) if  T implements I
fi(p) if *T implements I 

fp(p) or fp(&o) does not work at all, cause i dont know, how to implement *I
looks little strange to me.

if T implements I than, p*T references/pointsto something implementing I, and
  func(i*I){}(p)  and
  func(i I){}(o) should work
?!

why not something like 'struct T implements I {}'
could be checked and user of T would know what T can do / where T can be used (what T is intended to be)

if there is a type T implementing about 20 methods
do i have to search all interfaces for what interface actual is implemented by this type ? 
check for I asking for m1
check for I asking for m2
check for I asking for m1 and m2 .. (20 methods, lot of possibilities..)

thanks++, Andi


Axel Wagner

unread,
Aug 18, 2025, 12:42:07 PMAug 18
to andreas graeper, golan...@googlegroups.com
On Mon, 18 Aug 2025 at 18:25, 'andreas graeper' via golang-nuts <golan...@googlegroups.com> wrote:
type I interface { m()bool }
type T struct { x bool }
 func (o T) m()bool { return o.x }    // o:object
 func (p *T) m()bool { return p.x }   // p:pointer

both does not work. it is not possible to implement I for T and *T ( m already defined )

The value-receiver method will be promoted to the pointer type. So if you only define
func (T) m() bool
both the pointer and the value type implement I: https://go.dev/play/p/KhlC7QK0poH
 

func fi(i I){}
func fp(i*I){}

fi(o) if  T implements I
fi(p) if *T implements I 

fp(p) or fp(&o) does not work at all, cause i dont know, how to implement *I
looks little strange to me.

if T implements I than, p*T references/pointsto something implementing I, and
  func(i*I){}(p)  and
  func(i I){}(o) should work
?!

why not something like 'struct T implements I {}'
could be checked and user of T would know what T can do / where T can be used (what T is intended to be)

if there is a type T implementing about 20 methods
do i have to search all interfaces for what interface actual is implemented by this type ? 
check for I asking for m1
check for I asking for m2
check for I asking for m1 and m2 .. (20 methods, lot of possibilities..)

thanks++, Andi


--
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...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAFxc85Wz2Jozsx_fsJJD2Kca64EHivF5w-P_54VQw%3DKTZY6UuA%40mail.gmail.com.

Ian Lance Taylor

unread,
Aug 18, 2025, 12:52:15 PMAug 18
to andreas graeper, golan...@googlegroups.com
On Mon, Aug 18, 2025 at 9:25 AM 'andreas graeper' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> type I interface { m()bool }
> type T struct { x bool }
> func (o T) m()bool { return o.x } // o:object
> func (p *T) m()bool { return p.x } // p:pointer
>
> both does not work. it is not possible to implement I for T and *T ( m already defined )
>
> func fi(i I){}
> func fp(i*I){}
>
> fi(o) if T implements I
> fi(p) if *T implements I
>
> fp(p) or fp(&o) does not work at all, cause i dont know, how to implement *I
> looks little strange to me.

If T has a method m, then *T has the same method (see
https://go.dev/ref/spec#Method_sets). So given

func (o T) m()bool { return o.x } // o:object

then both type T and type *T implement I.

If that doesn't help, what are you really trying to do?

Ian
Reply all
Reply to author
Forward
0 new messages