huh? doesn't go's interfaces already allow for 'mixins'?
--
=====================
http://jessta.id.au
The main difference, to me, is only in the amount of typing involved.
If you have a class, like in Java, with 10 methods and you wanted to use
it like a mixin to delegate to it for its functionality, you have to
right 10 more methods each time you choose to do this. If the language
supported mixins directly, it could be a single statement. Basically, I
like to type as little as possible when I code. Mostly because my boss
kills my time making me fill out these ridiculous TPS reports.
Now that I think about it, I guess what I really want are "traits" and
not "mixins". Don't get me wrong, however, I would be fine with just
"mixins". Just looking for some additional power.
Seems like you can get that fairly easily just by merging another type
with your current type. E.g.
type Foo struct {
sync.Mutex
}
var f Foo
f.Lock()
f.Unlock()
So just by declaring the mutex type in Foo, you can now use Mutex
methods on it. If that's not the type of thing you're talking about,
then a more concrete example might be in order.
-- James
As I understand, lack of inheritance allows to keep type system
simple, and Go language designers will never add this feature.
- Sergei
P.S. Limited mixin-like effect can be achieved by type embedding. That
is, if struct A embeds struct B, then all methods of B are also
methods of A.

Seems like you can get that fairly easily just by merging another type
with your current type. E.g.
type Foo struct {
sync.Mutex
}
var f Foo
f.Lock()
f.Unlock()
So just by declaring the mutex type in Foo, you can now use Mutex
methods on it. If that's not the type of thing you're talking about,
then a more concrete example might be in order.
-- James
To unsubscribe from this group, send email to golang-nuts+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Hey, thanks. It is close to what I was discussing; though, I suspect it does not go as far as I was wanting (http://en.wikipedia.org/wiki/Trait_(computer_science)). Is there a way to do that dynamically or hide some of the methods behind an interface? I guess I should spend more time with the language and try it out.
What if I merged another type with some of the functions named the same? Well, I answered that one myself:
I believe you can disambiguate by adding the type names (e.g.
f.Mutex.Lock()). But I assume that in other languages this would
result in some kind of conflict as well. In fact, if it didn't it
could only confuse users.
Btw, there is a go.pastie.org where you can put code snippets.
-- James
Yes, you can't call a method with multiple definitions at the same
depth. There's no way to pick one. You can specify which one you want
though (eg. t.Mutex.Lock()), and you can wrap embedded methods at the
upper level to choose which one you want called.
Once you start getting into method exclusion, it starts to get tricky.
You can embed an interface, but then you are responsible for
initializing that interface in a factory method to a type that
actually implements the methods.
type foo interface { Foo() int }
type T struct { foo }
For example, you could now embed a var of T in itself, and then a call
to foo would be unresolvable at runtime, but would pass compilation.
Also, an embeded type acts as it would normally, ie it calls it's own
methods, not overriden ones. It can, of course, contain an interface:
type mixin struct { self interface {Foo()} }
And only call methods on self, letting mixin's embedders override
methods by embedding themselves in mixin. Again, this requires a
factory function to make sure everything is linked up properly.
A better approach is just to break your functionality into unitary
components that you can combine as needed, which can greatly simplify
your design compared to trying to pick subsets of the functionality
out of larger types and having to override other parts for variety.
A better approach is just to break your functionality into unitary
components that you can combine as needed, which can greatly simplify
your design compared to trying to pick subsets of the functionality
out of larger types and having to override other parts for variety.
> PS: Where in the documentation is embedding discussed as James Aguilar
> demonstrated in his earlier post? I've scanned the language spec several
> times and must have missed it.
http://golang.org/doc/effective_go.html#embedding
http://golang.org/doc/go_spec.html#Struct_types
Ian
A better approach is just to break your functionality into unitary
components that you can combine as needed, which can greatly simplify
your design compared to trying to pick subsets of the functionality
out of larger types and having to override other parts for variety.
I don't necessarily disagree with you. I am trying to conceptualize something simple like an observer pattern. Let me preface by saying I work in Java 98% of the time; one of my least favorite things, literally, is writing listeners and event code. I despise it. So, I go to great lengths to make it as painless as possible because it is repetitive, verbose, and tedious. So, I've created a set of generic components which I rely on about 80% of the time to provide this functionality. What is the "Go" way to implement the observer pattern? This relates to the mixin/trait discussion and how I've conceptualized wanting to solve this problem and my hope that it is more elegant in Go.
Now that I think about it, I guess what I really want are "traits" andnot "mixins". Don't get me wrong, however, I would be fine with just
"mixins". Just looking for some additional power.