Just to quote the spec <https://go.dev/ref/spec#Struct_types> to clarify,
perhaps:

> A field or method f of an embedded field in a struct x is called promoted
if x.f is a legal selector that denotes that field or method f

So there are two conditions for a method to be promoted: 1. it must be a
legal selector (this is given for the example) and 2. that selector must
denote the method.

To check whether the selector `m.EqualName` (if `m` has type `meter` or
`*meter`) denotes this field or method, we look up the rules for selector
expressions <https://go.dev/ref/spec#Selectors> (emphasis mine):

> For a value x of type T or *T where T is not a pointer or interface type,
x.f denotes the field or method *at the shallowest depth* in T where there
is such an f. If there is not exactly one f with shallowest depth, the
selector expression is illegal.

The field `meter.EqualName` has a more shallow depth than
`meter.EqualName.EqualName`, thus `m.EqualName` denotes the field, not the
method.
Thus, the method is not promoted. Thus `meter` has no `EqualName` method,
as the only way it could get one is by promotion.


On Thu, 27 Feb 2025 at 22:46, Ian Lance Taylor <i...@golang.org> wrote:

> On Thu, Feb 27, 2025 at 9:49 AM cpu...@gmail.com <cpui...@gmail.com>
> wrote:
> >
> > Thanks Ian, you are of course correct- but I still don't get it. Thank
> you for taking the time.
> >
> > > m.EqualName has to mean something. The Go language defines it as
> > meaning the field in m's type, rather than the embedded method.
> >
> > Doesn't have meter an embedded field of type EqualName (the interface)?
>
> It has an embedded field whose name is EqualName and whose type is
> EqualName. It's though the code said
>
> type meter struct {
>     EqualName EqualName
>     DifferentNamer DifferentNamer
> }
>
> except that methods and fields of the embedded types are promoted.
>
>
> > > Therefore, the type meter does not have a method EqualName. Therefore,
> > the type meter does not implement the EqualName interface.
> >
> > meter doesn't. But impl's methods get promoted to meter- which happens
> with different name: clearly the different name method has become promoted,
> implements the different namer interface and is non-nil.
> >
> > I still don't fully understand why this doesn't work for equal name. If
> impl's equal name method does get promoted it should implement the
> interface since meter now as a equal name method. If it doesn't (due to
> name conflict with the embedded equal name interface) where would still be
> the embedded equal name interface (which might be nil but still existing)?
>
> As you say, if the EqualName method were promoted it would conflict
> with the existing field of type EqualName. Therefore, it is not
> promoted. Therefore, meter does not have a method named EqualName. It
> has a field named EqualName instead. Therefore, meter does not
> implement the interface EqualName. It doesn't have a method EqualName,
> so there is no way that it could implement the interface EqualName.
>
> Ian
>
> --
> 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 visit
> https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWf31G9fDT96qPfcPdnZhFXnYTvzpZpW_cYjAT_oAk0SA%40mail.gmail.com
> .
>

-- 
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 visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEXJSvew461xSLOODa%2B5zZPYk7USeV6va6x%3Dj%3DJqUUe%3Dg%40mail.gmail.com.

Reply via email to