On Wed, Sep 16, 2020 at 10:51 AM Stephan Lukits <stephan.luk...@gmail.com>
wrote:

> Assume a public interface I with two methods NotInS and M whereas NotInS
> is calling M.  An implementation i of I which implements NotInS and M as
> well as a specialization s of i which only implements M.  Finally a
> constructor whose return-type is I returns a pointer to an instance of
> s.  Calling on this return-value NotInS will execute i.M() as the
> following test shows.  I would like it to call s.M(). How would I do
> this in a goish way?


You can't. What you want is definitionally not "goish". Go doesn't have a
way to "specialize" a type or overload methods. If you want a method of
`*i` to call a method of another value, you need to pass that value to that
`*i` somehow.

Your broader problem likely has a solution in Go, but you narrowed down the
definition of what you want so far, that it's not achievable in Go.



>   Thanks for any help!
>
> package main
>
> import (
>         "fmt"
> )
>
> type I interface {
>         NotInS()
>         M()
> }
>
> type i struct{}
>
> func (_i *i) NotInS() {
>         _i.M()
> }
>
> func (_i *i) M() {
>         fmt.Println("i")
> }
>
> type s struct{ *i }
>
> func (_s *s) M() {
>         fmt.Println("s")
> }
>
> func newI() I {
>         return &s{&i{}}
> }
>
> func main() {
>         _i := newI()
>         _i.NotInS() // prints i instead of s
> }
>
> --
> 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/fb4e1386-4c24-6486-58b3-93dd07e46259%40gmail.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 on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGrpmCk6BNm2VXr73vwc5VfhhmduzM_mVhv9sCePaCX-Q%40mail.gmail.com.

Reply via email to