I don't see the problem. The interface should just look if such a function exists. And if it exists then the object has such an interface. Where is the problem in joining multiple interfaces if the containing functions do the same stuff?
But why is that a problem. The interface is for duck typing which means that this is more like a pattern matching. If a has a pattern already he should ignore the duplicate.
Why does the interface declares two identical methods.
Is there another solution?
On Saturday, March 10, 2012 4:08:39 PM UTC+1, r2p2 wrote:Why does the interface declares two identical methods.Well, you have explicitly declared the duplicity. I'm lost ;-)> I would see this as an: If you (struct) want to have this interface, you have to> implement these methods. Well if there are two he has to ignore one.I've no idea how this is meant. There is no way know to me how an interface can have duplicate methods (what would be the semantics???), so what 'ignore one of the duplicates' means??
Is there another solution?
Sure. Design your interface w/o duplicate methods. I don't mean it in any bad way, it's the only option available ;-)
Do I see this right that you don't know my code to get this error message? In my first post is a link to golang playground which contains this code. In case the link was filtered: There it is
type hasType interface {Type() byte}type incomingMessage interface {hasTypeReadFrom(w io.Reader) error
}
type outgoingMessage interface {hasTypeWriteTo(w io.Writer) error}
type inOutMessage interface {incomingMessageoutgoingMessage}
--Benny.
> I agree with you r2p2, it's should work according to the duck typing philosophy. I think it's a bug.
It is not a bug, it is working as intended and specified. Interfaces aren't defined based on some "duck typing philosophy", they are defined as the language authors saw fit.
The problem with just ignoring duplicates is that the methods may actually come from different places and mean different things (though the serendipity of this seems unlikely). An implementer may then not know which spec to follow. If this situation arises, the person defining the interface should make their intent explicit in the code.
Plausibly, the language could recognize that the method names originate in the same method spec and allow it. I think that would make sense and help in this case.
I think that's a valid question for the Go team.
This
interface { MyMethod() MyType; MyMethod() MyType }
is pretty clearly a mistake (even if you have a semantics which
just drops duplicates, as the term `set` would imply).
If you want to catch this, BUT allow other routes to duplication,
you're going to have non-trivial rules to decide what counts as
a problem or not.
I can easily see that Go team deciding that such rules were
not Simple and would mean more time spent maintaining
the spec and compilers and handling questions on the list.
Chris
--
Chris "allusive" Dollin
But you need only one interface, and one struct to create such an incompatibility.
<snip>
Cube is not realy a cowboy but you can pass it to do_something. You have to be sure that you use an interface like intended but the language doesn't force you.
If we ignore this for a second and try to believe that one interface has an intention, than this intention should not change if the interface stays the same. If both interfaces Cowboy and Artist would include one interface (eg. Drawable) the intention of draw() should stay the same. So the compiler could allow this because Artist and Cowboy use the draw with the same intention. If not they could include different interfaces (eg Drawable and DrawableWeapon [both would contain the draw method]).
> I agree with you r2p2, it's should work according to the duck typing
> philosophy. I think it's a bug.
In general, checking for conflicts in interface type definitions is a
good idea because there might be an unintended ambiguity. But given
that the method comes from the same declared interface type, the error
is indeed rather pointless here. On the other hand, the error is
suppressed in such cases, the rules for a valid interface definition
become pretty involved. So it seems to me that there isn't a
compelling case to change the compiler, after all.
> May we ask the language designers for clarification on this possible issue?
Speaking purely personally, the current spec is quite simple. And I
don't think we should permit
interface {
func F()
func F()
}
So the question is: do we get an advantage by complicating the spec to
support this case. And my personal answer is that this is an unusual
edge case, and it's not worth it.
Ian
Johann Höchtl <johann....@gmail.com> writes:
Speaking purely personally, the current spec is quite simple. And I
don't think we should permit
interface {
func F()
func F()
}
So the question is: do we get an advantage by complicating the spec to
support this case. And my personal answer is that this is an unusual
edge case, and it's not worth it.
Ian
In a type's method set, each method must have a unique method name, unless the type is an interface and the duplicate method names originate in the same method spec.
Two named types are identical if their type names originate in the same type declaration.
Two named types are identical if their type names originate in the same type spec.
I think what they want is "diamond multi-inheritance" for interfaces. Is that correct?
I have a similar use case and just hit the same issue. Is this issue being tracked yet in the golang issue tracker, or still being hashed out here in this thread?
On Mon, Apr 30, 2012 at 12:30 PM, <eik...@eikeon.com> wrote:I have a similar use case and just hit the same issue. Is this issue being tracked yet in the golang issue tracker, or still being hashed out here in this thread?I think the consensus was that it's working as intended.
type RPCCodec interface {rpc.ClientCodecrpc.ServerCodec}func NewSimpleRPCCodec(...) RPCCodec { ... }func NewCustomRPCCodec(...) RPCCodec { ... }// ...
type RPCCodec interface {// write out all ClientCodec methods, because both have Close() meaning they both cannot be embeddedWriteRequest(*rpc.Request, interface{}) errorReadResponseHeader(*rpc.Response) errorReadResponseBody(interface{}) error// rpc.ClientCodecrpc.ServerCodec}
I just came across a need for this again and wanted to share what I had to do.I have code that implements returns implementations of rpc.ClientCodec and rpc.ServerCodec, but don't want to export the underlying types. So I had:type RPCCodec interface {rpc.ClientCodecrpc.ServerCodec}func NewSimpleRPCCodec(...) RPCCodec { ... }func NewCustomRPCCodec(...) RPCCodec { ... }// ...Unfortunately, both rpc.ClientCodec and rpc.ServerCodec implement Close() error method. So I had to do instead:type RPCCodec interface {// write out all ClientCodec methods, because both have Close() meaning they both cannot be embeddedWriteRequest(*rpc.Request, interface{}) errorReadResponseHeader(*rpc.Response) errorReadResponseBody(interface{}) error// rpc.ClientCodecrpc.ServerCodec}