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
Summary:
I propose allowing interfaces to have exported fields, e.g.
type I interface {
X int
}
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.
Proposal:
Allow interfaces to have fields.
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.
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.
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.
Implementation: TBD.
The text was updated successfully, but these errors were encountered:
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.
Summary:
I propose allowing interfaces to have exported fields, e.g.
Background:
The go way of setting fields in a struct is to directly access them. E.g. if we have a struct like
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:
If want to use F with other strucutes but the obvious way is to accept an interface
but this doesn't work because we cannot have an interface with a field.
Proposal:
Allow interfaces to have fields.
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.
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.
Alternatives:
The text was updated successfully, but these errors were encountered: