Hey folks, what a great discussion.

To be clear, if something like that would exist, it should have obvious 
limitation, like any Go features regarding structs and interfaces.

It would be illegal to do that for instance :

type Foo interface {
    Do()
}
func (f Foo)interface Do(){}

Now I don't think it would make code harder to read. If Foo is used as an 
argument :

func AcceptFoo(f Foo){
   f.Do() // Execute Foo.Do
}
If Do is not inside the Foo "contract" but a method of Foo, there is 
ambiguity as to what is called.

Even if a struct implementing Foo has a Do function of his own, AcceptFoo 
cannot know that
since it is not defined on the interface .

When it comes to methods on structs, nothing would change : 

type Bar struct {}
func(b Bar)Do()

bar := Bar{}

bar.Do() // execute Bar.Do()

// however 

var bar2 Foo = Bar{}

bar2.Do() // execute Foo.Do()


A strict behavior should be defined one way or another, but I think this 
one would be interesting. 

Anyway It was just a thought on the matter, no big deal. It would be a 
great way to encapsulate some behavior without exposing internals or using 
package level functions accepting interfaces thus leading to cleaner API 
for libraries. If you think about technical implications or some current Go 
features this idea might break, let me know.




Le mardi 18 octobre 2016 20:12:17 UTC+2, parais...@gmail.com a écrit :
>
> Obviously in Go this is a compile time error :
>
> type Foo interface {}
>
>
> func (f Foo) DoSomething(){}
>
>
> I wonder if there is some technical limitations that make it undesirable 
> or hard to implement, or it's just that Go designers didn't think it is a 
> relevant feature.
>
> I personally think there is it could be handy in some situations, instead 
> of having to write a function without receiver, it could allow to attach 
> behavior to interfaces automatically 
> instead of the explicit :
>
> func DoSomething(f Foo){}
>
>
> what is your opinion on the matter ? Unfortunately I wasn't able to catch 
> anyone of the Go team during the recent conference in Paris to ask that 
> question.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to