Wednesday, May 15, 2024
HomeGolangCalling the embedding struct's overloaded methodology from the embedded struct - Technical...

Calling the embedding struct’s overloaded methodology from the embedded struct – Technical Dialogue


Contemplate this instance:

package deal primary

import (
    "fmt"
)

kind A struct {}

func (a *A) TheMethod() {
    fmt.Println("TheMethod (A's implementation)")
}

kind B struct {
    A
}

func (b *B) TheMethod() {
    fmt.Println("TheMethod (B's implementation)")
}

func (a *A) TheMethodCallingTheMethod() {
    a.TheMethod()
}

func primary() {
    foo := B{}
    foo.TheMethodCallingTheMethod()
}

The output is TheMethod (A's implementation) which is sort-of comprehensible.

Nevertheless, shouldn’t it ideally be TheMethod (B's implementation)?

The rationale is that B already can transparently see A’s strategies. So, why would A’s TheMethodCallingTheMethod (when known as from an occasion of B) not be capable to transparently see that it’s B calling it, not A, and therefore, name B’s TheMethod not A’s?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments