Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: allow exported fields in interfaces (Go2) #19524

Closed
ghasemloo opened this issue Mar 13, 2017 · 1 comment
Closed

Proposal: allow exported fields in interfaces (Go2) #19524

ghasemloo opened this issue Mar 13, 2017 · 1 comment
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal

Comments

@ghasemloo
Copy link

ghasemloo commented Mar 13, 2017

Summary:
I propose allowing interfaces to have exported fields, e.g.

type I interface {
  X int
}
  1. Background:
    The go way of setting fields in a struct is to directly access them. E.g. if we have a struct like

    type S struct {
      X int
    }
    

    we do not define setter and getter methods for X, we use .X to directly set and get X. E.g. protobuf code generated for go does not have setters and getters for their fields (unlike other languages).
    Consider a function that needs to access a field:

    func F(s S) {
      s.X = 5
    }
    

    If want to use F with other strucutes but the obvious way is to accept an interface

    func F(i I) {
      i.X = 5
    }
    

    but this doesn't work because we cannot have an interface with a field.

  2. Proposal:
    Allow interfaces to have fields.

  3. Impact:
    Code using reflection on interface types might be effected when the extended interfaces are passed to them. Otherwise there should not be an effect on existing code.

  4. Discussion:
    Interfaces correspond to behavior. Either fields are not part of behavior of objects in which case we should be using setters and getters like other languages or they are part of the behavior in which case we should treat them as such and allow them in interfaces.

  5. Alternatives:

  • Alternative 1: use reflection (too much complexity to do something very simple, has the drawbacks of using reflection).
  • Alternative 2: define interfaces with setters/getters
    type I interface {
      SetX(int)
    }
    
    and define setter and getter methods for each struct:
    func (s *S) SetX(x int) {
      s.X = x
    }
    
    If the type is defined in another package we would need to redefine it as a new type.
  1. Implementation: TBD.
@bradfitz
Copy link
Contributor

Sorry, Go isn't making language changes at this time. The language was pretty frozen at Go 1.0, and has become increasingly frozen with each release.

I'll tag this LanguageChange for future consideration for any Go 2.0 where it can be considered together with all other language change proposals. (Go doesn't do ad hoc language changes without considering how they all interact together) There have been similar proposals in the past, though. I seem to recall one where the proposer named them "data faces" or something, but I can't find it back now. Maybe somebody else has a link.

@bradfitz bradfitz added LanguageChange Suggested changes to the Go language Proposal labels Mar 13, 2017
@ghasemloo ghasemloo changed the title Proposal: allow exported fields in interfaces Proposal: allow exported fields in interfaces (Go2) Jul 23, 2017
@golang golang locked and limited conversation to collaborators Jul 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal
Projects
None yet
Development

No branches or pull requests

3 participants