You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you view an interface as a set of constraints on the implementing type, then
combining two interfaces (that are not mutually incompatible) such as:
type I interface { f(); String() string }
type J interface { g(); String() string }
has a natural interpretation that is equivalent to an interface containing the union of
such constraints. e.g. these should be equivalent:
type IJ interface { I; J }
type IJ interface { f(); g(); String() string }
but in fact the first is an error: "duplicate method: String". This is
somewhat surprising. Is there any reason not to permit this? The set-union behaviour
is easy to understand, describe and implement, and it seems useful in practise when you
have overlapping interfaces describing different aspects of a type.
(I chose String() string since I've seen many users add this constraint to their
interfaces. It could be any method though.)
Reacted by Joe Tsai, Caleb Spare, Leigh, Prashant V, Robert Griesemer, Bjarke Ebert, Kirill Smelkov, Bryan C. Mills, Kostas Christidis, Michal Štrba and 106 more
Reacted by Konstantin, Doyle, toukii and josephGuo
Which String method gets called when I do
var x IJ // using first definition
fmt.Println(x.String())
The resolution of the ambiguity is why the second version of IJ works.
Reacted by as and Andrés M.
Reacted by Alexander Trakhimenok, Deepak Jacob, EndWithHyphen, andrea denisse, Yann Berthou (firetonton), Sergey Linnik, Jérôme Laforge, Christopher Hlubek, Martins, F. and Noah S-C
Note that even if you factor out the common method into its own interface it doesn't
work:
type C interface { String() string }
type CI interface { C; f() }
type CJ interface { C; g() }
type CIJ interface { CI; CJ }
http://play.golang.org/p/r5yOykMn-a
prog.go:8: duplicate method String
Re: #7: you didn't factor out the common method in CIJ - both CI and CJ have the String
method and hence the same issue.
Factoring out a common method set will work.
Reacted by jimmyfrasche, sāmīr, Oleg Kovalov, Tom Thorogood, Chris Miller, Levente Liu, Tamás Demeter-Haludka, Alfred Zhong, Gerasimos (Makis) Maropoulos, Oleg Kislitsin and 9 more
Reacted by Bryan C. Mills, Ben Visness, Tom Thorogood, Paul Costinean, Sergii Getman, 灵茶山艾府 [0x3F] and Simone Basso
Activity
robpike commentedon Dec 18, 2013
Comment 1:
adonovan commentedon Jan 2, 2014
Comment 2:
griesemer commentedon Jan 8, 2014
Comment 3:
Labels changed: added release-none, languagechange.
Owner changed to @griesemer.
leo-liu commentedon Jan 24, 2014
Comment 4:
griesemer commentedon Feb 27, 2014
Comment 5:
Status changed to LongTerm.
griesemer commentedon Apr 21, 2014
Comment 6:
Labels changed: added repo-main.
gopherbot commentedon May 21, 2014
Comment 7 by SRabbelier:
Note that even if you factor out the common method into its own interface it doesn't work: type C interface { String() string } type CI interface { C; f() } type CJ interface { C; g() } type CIJ interface { CI; CJ } http://play.golang.org/p/r5yOykMn-a prog.go:8: duplicate method Stringgriesemer commentedon May 21, 2014
Comment 8:
122 remaining items
[-]proposal: spec: allow embedding overlapping interfaces[/-][+]spec: allow embedding overlapping interfaces[/+]rsc commentedon Nov 13, 2019
Already implemented, so closing.
gopherbot commentedon Jan 10, 2020
Change https://golang.org/cl/214240 mentions this issue:
compiler: permit duplicate methods from embedded interfacescompiler: permit duplicate methods from embedded interfaces
compiler: permit duplicate methods from embedded interfaces
compiler: permit duplicate methods from embedded interfaces
gopherbot commentedon Jan 30, 2020
Change https://golang.org/cl/216997 mentions this issue:
doc/go1.14: document overlapping interfaces change (update release notes)doc/go1.14: document overlapping interfaces change (update release no…
gopherbot commentedon Jan 31, 2020
Change https://golang.org/cl/217134 mentions this issue:
go/types: unexport Checker.LookupFieldOrMethodgo/types: unexport Checker.LookupFieldOrMethod