Skip to content

spec: allow embedding overlapping interfaces #6977

Closed

Description

@adonovan
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.)

Activity

  1. robpike commented on Dec 18, 2013

    @robpike
    Contributor

    Comment 1:

    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.
  2. adonovan commented on Jan 2, 2014

    @adonovan
    MemberAuthor

    Comment 2:

    But does that matter?  You could choose one arbitrarily and the effect would be the same.
  3. griesemer commented on Jan 8, 2014

    @griesemer
    Contributor

    Comment 3:

    Labels changed: added release-none, languagechange.

    Owner changed to @griesemer.

  4. leo-liu commented on Jan 24, 2014

    @leo-liu

    Comment 4:

    There's no ambiguity at all. IJ is a interface but not a struct, so no real method is
    duplicated.
  5. griesemer commented on Feb 27, 2014

    @griesemer
    Contributor

    Comment 5:

    Status changed to LongTerm.

  6. griesemer commented on Apr 21, 2014

    @griesemer
    Contributor

    Comment 6:

    Labels changed: added repo-main.

  7. gopherbot commented on May 21, 2014

    @gopherbot
    Contributor

    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 String
  8. griesemer commented on May 21, 2014

    @griesemer
    Contributor

    Comment 8:

    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.
  9. added this to the Unplanned milestone on Apr 10, 2015
  10. 122 remaining items

  11. changed the title [-]proposal: spec: allow embedding overlapping interfaces[/-] [+]spec: allow embedding overlapping interfaces[/+] on Nov 13, 2019
  12. rsc commented on Nov 13, 2019

    @rsc
    Contributor

    Already implemented, so closing.

  13. gopherbot commented on Jan 10, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/214240 mentions this issue: compiler: permit duplicate methods from embedded interfaces

  14. gopherbot commented on Jan 30, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/216997 mentions this issue: doc/go1.14: document overlapping interfaces change (update release notes)

  15. gopherbot commented on Jan 31, 2020

    @gopherbot
    Contributor

    Change https://golang.org/cl/217134 mentions this issue: go/types: unexport Checker.LookupFieldOrMethod

  16. locked and limited conversation to collaborators on Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.ProposalProposal-Acceptedv2An incompatible library change

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions