Closed henryas closed 3 months ago
(I assume you mean removing embedded types in general, rather than just embedded structs, which are simply a common type to embed.)
Without casting any judgement on this proposal, I note that embedding is also a significant cause of complexity in the language and implementation.
I agree with your presentation of the limitations but disagree with your conclusion to remove it from the language. Embeddings are useful (and the limitations negligible) when you have lots of little types 'embedding' off another little one. It is far less cognitive load to think of subwoofer
as speaker plus wubdubdub
. I agree this is the wrong approach when you have really long recievers and complex composition though, then interfaces are a much better option.
Looking at your alternative, which you wrote as:
func Drive(driver Driver, vehicle Vehicle) {}
Imagine the realistic case where Driver
or Vehicle
have obscure interface methods, and there are a lot of implementations of Driver
and/or Vehicle
. Even having forwarder methods as you propose is a lot of duplication to deal with for function spec changes to the forwarded method (less of a problem nowadays with smart search-and-replace with IDEs).
Lastly, how often have we seen embedding used horrifically? I've seldom seen it (feel free to prove me wrong with links to stuff I'll need to follow up with eye-bleach), and I say if it aint broke dont fix it (especially considering it represents an additional break from Go1 compatibility).
I would like to propose the removal of embedded struct in Go 2.x, because it does not solve inheritance problems, and the alternatives may actually be better.
Yes, it does not solve inheritance problems, because embedding is not intended to solve inheritance problems. Because it does not solve what it was not intended to solve, the above rationale about why to remove it makes no sense to me.
I agree with neither the technical accuracy of the premise nor the final conclusion. Inheritance locks in functional dependencies in the base class. Embedding allows those dependencies to be overridden or propagated at a per-method granularity.
I frequently use interface embedding when only a few interface methods must be overridden:
// StatsConn is a `net.Conn` that counts the number of bytes read
// from the underlying connection.
type StatsConn struct {
net.Conn
BytesRead uint64
}
func (sc *StatsConn) Read(p []byte) (int, error) {
n, err := sc.Conn.Read(p)
sc.BytesRead += uint64(n)
return n, err
}
or
// AuthListener is a `net.Listener` that rejects unauthorized connections
type AuthListener struct {
net.Listener
}
func (al *AuthListener) Accept() (net.Conn, error) {
for {
c, err := al.Listener.Accept()
if err != nil {
return c, err
}
if authorizeConn(c) {
return c, nil
}
c.Close()
}
}
There is no need in implementing other net.Conn
and net.Listener
methods thanks to method forwarding in these cases.
@griesemer Yes. Embedded types is the more accurate term. I used embedded struct in the example because it is more evil compared to embedded interface. I would think that it shouldn't be that difficult to type out the full interface signature than relying on interface embedding.
@twitchyliquid64 Sorry if I didn't give a clear example earlier. What I meant to say was instead of doing the following:
type BaseDriver struct{}
func (b BaseDriver) Drive(vehicle Vehicle){}
type TruckDriver struct{
BaseDriver
}
type CabDriver struct{
BaseDriver
}
You have two alternatives. First, you can use the real composition and employ method forwarding, which would be as follows:
type TruckDriver struct{
parent BaseDriver
}
func (t TruckDriver) Drive(vehicle Vehicle){
t.parent.Drive(vehicle) //method forwarding
//...or you may implement custom functionalities.
}
It is true that it takes a little duplication (which requires little to no additional effort), but the given the benefit of having the TruckDriver decoupled from the BaseDriver and easier maintenance in the future, I say it's worth it.
Alternatively, you may refactor the common method into function. So the above code snippet will now look like this:
type Driver interface {} //which may be implemented by TruckDriver, CabDriver, etc.
func Drive(driver Driver, vehicle Vehicle){} //Drive is usable by any kind of driver.
The benefit of this approach is that each driver exists independently, and the Drive functionality is decoupled from any specific driver. You can change one or the other without worrying about unforeseen domino effect.
I prefer having my types flat rather than deep. The problem with type hierarchy is that it is difficult to tell what other things you may break when you make a slight alteration to your code.
Given that embedded types is a major feature in Go, I am actually surprised to see that it isn't used as much as I anticipate it to be. It may be due to its certain characteristics, or simply people having no particular need for it. At least, gophers don't go crazy with constructing the different families of apes (or may be not yet). Regardless, I don't agree with the "if it ain't broke, then don't fix it". I am more of a "broken windows theory" guy. Go 2.x is the opportunity to take lessons learned from Go 1.x and makes the necessary improvements. I would think that the resulting simplicity from the removal of unnecessary features may bring about other future opportunities (eg. other potentially good ideas that may be difficult to implement due to the complexity of the current implementation).
@cznic It behaves exactly like inheritance. Changes to the base type is automatically inherited by the derived types. If it does not solve the problems that inheritance is meant to solve, then I don't know what does.
@as I have no idea what you are talking about. Here is an illustration. The last I checked my Parents were normal human beings. Since I am meant to function as a normal human being, I inherit from them so that I too can be a human. However, one day, my Parents abruptly decide to grow scales and a pair of wings, and breath fire. Since I inherit from them, I am too automatically turned into a dragon (without my permission!). Since I am expected to function as a human, it breaks my intended functionality. I could insist in being human by overriding the inherited behavior from them. However, no one can tell what other crazy things they may do in the future that will again break my functionalities. That is doable in both inheritance and in Go's embedded types.
It behaves exactly like inheritance. Changes to the base type is automatically inherited by the derived types.
It does not behaves like inheritance at all. Given
type T int
func (T) foo()
type U struct {
T
}
var u U
u.foo()
behaves exactly like u.T.foo()
when there's no foo
defined within U
- that's composition, not inheritance. It's only syntactic sugar with zero semantic change. When (T).foo
executes it doesn't have the slightest idea its receiver is emebded in some "parent" type. Nothing from T
gets inherited by U
. The syntactic sugar only saves typing the selector(s), nothing else is happenning.
@cznic I agree with you that T is embedded in U and that is composition. However, the real issue is with the automatic propagation of T characteristics (even if those characteristics are still technically belong to T). Now, in order to learn what I can do with U (as a consumer of U), I have to learn what T does as well. If T changes, I as the consumer of U, have to be aware of those changes as well. This is the inheritance-like behavior I was talking about. It is simple if it involves only T and U. Imagine if T embeds X, and X embeds Y, and Y embeds Z. All of a sudden, you get the monkey and the banana problem.
In order for any proposal along these lines to be adopted, you'll need to do some analysis of where and how current Go programs use embedding, and how difficult it would be for them to adapt. You could start with just the standard library.
@henryas
Since I am expected to function as a human, it breaks my intended functionality.
No, it enhances your functionality. If your intended behavior is to be Human, there is nothing the embedded type can do to change that or take it away. Sure, for some reason if the original type gained a method
BreatheFire(at complex128)
You could say you're say you're a dragon, but you're not. You're a fire breathing human who appreciates complex numbers. Your concerns seem to be about the general concept of functional dependency than about the inheritance problem. These exists in packages, interfaces, function parameter lists, and the list goes on.
The base struct implementor may inadvertently break the derived structs.
That claim is not obvious to me. Could you give some concrete examples?
@ianlancetaylor I'll see what I can do.
@as It's actually pretty cool to be a fire-breathing human once in a while. But how much I will stay as a human depends on my earlier expectation of my Parents. If I thought that my Parents were already humans and therefore I didn't implement much humanity in me (because it was already implemented by my Parents), when my Parents turn into a giant lizard, I too will turn. Basically, I don't become what I expect myself to be, but I am actually the enhanced version of my Parents (whatever my Parents want to be). You may argue that this is technically the correct definition of inheritance (and it is), but it breaks the original assumption about me. The assumption about me depends on my assumption about my Parents, whereas my Parents don't necessarily know (and don't need to!) my assumption about them.
The main issue is that I don't encapsulate my Parents trait in me. When my client deals with me, they also have to deal with my Parents, and possibly my Grandparents as well. It creates unnecessary coupling and breaks encapsulation. Changes in one will have a domino effect on the others. With proper encapsulation, the effect of a change should be contained like ship compartmentation.
Again, it depends on whether this is the intended behavior that the Go's Team wants in Go language. If it is then I say they should not hesitate and support the proper inheritance all the way. After all, despite its criticism, inheritance is still a major feature in many popular programming languages today. If they do not want it, then they should scrap it and I vote for scrapping it.
It does not take much effort to write forwarding methods.
That is only true at small scales: at larger scales, the advantage of embedding is that you do not need to update forwarding for each newly-added method.
If you embed a BaseDriver
today and someone adds some new method to it (e.g. PayToll(amount Money) error
), all of the embedders will now implement the expanded method set too. On the other hand, with explicit forwarding methods you must locate and update all of the embedders before you can (say) add that method to a corresponding interface type.
@bcmills The problem with automatic method implementation is that the people who write BaseDriver
may not know the context in which BaseDriver
are used by their derived drivers, and may inadvertently break the original assumption about the specific drivers. For instance, in your example, I may have certain drivers whom I may want them to pay toll using cash card that the company can control, rather than with cash. I may already have EnterTollRoad(card *CashCard)
method in my driver. The changes you make to the BaseDriver
just provided a way to circumvent the security measure I have in place for my specific driver.
@henryas I don't personally find that to be a particularly convincing argument. The fact that it is possible for a program to misuse some feature of the language does not imply that that language feature should be removed. You need to a longer argument; for example, you might argue that the misuse is a common error that people routinely make.
@ianlancetaylor if you read my earlier posts, I did point out that ... well, I will summarize it again here:
By the way, those are common arguments against inheritance and none of them is my invention. So I really don't need to prove their validity and how they apply in practice. They have been around for years, expounded by experts and practitioners alike. They are relevant to Go's embedded type because despite it not being technically an inheritance, it exhibits an inheritance-like behavior that causes it to share the same pros and cons of an inheritance.
As mentioned before, despite arguments against inheritance, inheritance is still widely used in many programming languages today. So it's really up to the Go's Team to decide which direction they want to take Go language to. In my opinion, embedded type is not a needed feature and can be removed from the language.
However, should the Go team decide to keep this feature, I would say there is no need to complicate it by making it look like an inheritance in disguise. After all, you are still getting all the side effects of a proper inheritance anyway. Just take the common road to inheritance, and boldly claim this is inheritance. It is simpler that way and you most likely have all the kinks already ironed out by other languages who share the same road.
@henryas Thanks, but I'm not asking for a list of possible errors. I'm asking for an argument that these are errors that people commonly make.
To put it another way, every reasonably powerful language has features that can be misused. But those features also have a benefit. The question, as with all language questions, is how to weigh the benefits against the drawbacks. The fact that a feature can be misused is a minor drawback. The fact that a feature frequently is misused is a major drawback. You've demonstrated a minor drawback. I'm asking whether you can demonstrate a major one.
We don't call embedded types inheritance because it is not inheritance as most people use the term in languages like C++ and Java. For us to call it inheritance would be confusing. See also https://golang.org/doc/faq#Is_Go_an_object-oriented_language and https://golang.org/doc/faq#inheritance .
@ianlancetaylor What you are asking is difficult to quantify. How do you weigh one feature against another? How do you measure the benefits and the drawbacks, and whether they are major or minor? It isn't as simple as counting loc. The best that I can think of is by drawing opinions from everyone's experience with real-life projects whether they would rather have the feature or its absence.
In the Go projects that I have been involved with so far, there have been very little use of embedded types. If I recall correctly, we might have used embedded types for some small, simple types. We can live without the feature with no problem. It shouldn't take much effort to refactor the code. In fact, thinking back, it may be better to not use embedded types. However, considering the types involved are minor, small, and very unlikely to change, I would say, in our projects so far, it makes little difference between using the embedded types and not. Our team is also small. The longest distance between one person to another is just one table away. So the ease of communication may help in a way. In addition, the base and the derived types were usually written by the same person. So I would say it is quite difficult to judge the merits (or their absence) of embedded types from my own projects. However, I would think that embedded types is a superfluous feature.
About why I mention inheritance when the subject is about embedded types, I briefly mentioned this in the earlier post:
They are relevant to Go's embedded type because despite it not being technically an inheritance, it exhibits an inheritance-like behavior that causes it to share the same pros and cons of an inheritance.
I will elaborate further, and attempt to compare Go's embedded types to the common notions of inheritance and composition. I will use Parent and Child rather than Base and Derived.
What is inheritance? Inheritance is described as an "is-a" relationship between two types. The Parent defines the identity, and the Child is essentially the Parent with additional features. You can think of the Child as the enhanced version of the Parent. If the Parent is a fish, then the Child is a super fish. If the Parent is a bird, then the Child is a super bird.
How is this relationship implemented in practice? It is implemented by having the Parent's characteristics automatically exposed by the Child. Now let's compare this to Go's embedded types.
type Parent struct{}
func (p Parent) GrowFinsAndSwim(){} //Parent is a fish
//The child is automatically a fish
type Child struct{
Parent
}
func (c Child) TalkTo(person Person){}
//A super fish that can talk to human
child.GrowFinsAndSwim()
child.TalkTo(person)
Now, what if we changed the Parent to be a bird.
type Parent{}
func (p Parent) GrowWingsAndFly(){} //Parent is now a bird
//The child now becomes a bird
type Child struct{
Parent
}
func (c Child) TalkTo(person Person){}
//A super bird that can talk to human
child.GrowWingsAndFly()
child.TalkTo(person)
As seen from the above illustration, Go's embedded types behaves like inheritance. The Parent defines the identity, and the Child is the Parent with some enhancements.
Another aspect of inheritance is the Child's ability to override the Parent's behavior to provide more specialized behavior. We'll see how we can do that with Go's embedded type.
type Parent{}
func (p Parent) GrowWingsAndFly(){} //Parent is a bird
//The super bird
type Child struct{
Parent
gps GPS
}
func (c Child) GrowWingsAndFly(){}
//Unlike the Parent, this super bird uses GPS to fly.
child.GrowWingsAndFly()
Again, we see inheritance-like behavior in Go's embedded type.
Now, how do you implement inheritance? Different languages may have different implementations, but I would think that the Child would need to hold some kind of reference to the Parent in order for it to work. If you describe it in Go, it would look something like this:
type Child struct{
*Parent
}
Voila! It is embedded type! Is Go's embedded type actually an inheritance? You decide.
However, there is an important difference between Go's embedded type and the normal inheritance. The normal inheritance exposes only the Parent's behavior. On the other hand, Go's exposes both the Parent's behavior and the Parent itself. The question to ask is whether there is any benefit to doing so?
Now, we will compare Go's embedded type to composition. Composition is defined as a "has-a" relationship between two types. A Person has a Pencil, but the Person is definitely not a Pencil. The Child controls its own identity and the Parent has no control whatsoever over the Child. In Go, proper composition should look like this:
type Person struct{
pencil Pencil
}
func (p Person) Write (book Book) {
//do something with pencil and book
}
The important thing to note here is that Pencil is well encapsulated by Person. The definition of Pencil does not automatically apply to Person. If you try to use embedded type to describe this "has a" relationship:
type Person struct{
Pencil
}
type Pencil struct{}
func (p Pencil) Brand() string{}
func (p Pencil) Manufacturer() string{}
//Okay
person.Pencil.Brand()
//But ... Ouch!
person.Manufacturer()
person.Brand()
that very definition of "has a" (composition) will break, as embedded type resembles more of an "is a" (inheritance) relationship than a "has a" relationship.
Let's start by calling a spade a spade. Go's embedded type is inheritance. It describes an "is a" rather than a "has a" relationship. Technically, it is inheritance disguised as composition. The thing is why do it in a queer roundabout way if what you need is plain inheritance. Is there any benefit to doing so? I think Go 2.x is a great opportunity to fix it or ditch it.
Let's start by calling a spade a spade. Go's embedded type is inheritance
It is not.
The thing is why do it in a roundabout way if what you need is plain inheritance.
Go does not have inheritance, so embedding is not a round about way of providing inheritance as Go does not provide that feature. In Go value types satisfy the "is-a" relationship, and interface types satisfy the "has-a" relationship.
@davecheney Is the last sentence saying what you want to wrote? It seems to me it's the other way around. Or I'm really confused before the first coffee of the day.
@davecheney I am puzzled.
In Go value types satisfy the "is-a" relationship
If Go value types satisfy the "is-a" relationship, how do you describe "A Carpenter is a Person that build furniture. A Singer is a Person that sings" in Go value types?
and interface types satisfy the "has-a" relationship.
type Pencil struct{}
type Person struct{
pencil Pencil
}
func (p *Person) SetPencil(pencil Pencil){}
func (p *Person) Pencil() Pencil{}
The above code has no interface whatsoever, and yet the Person "has a" Pencil.
Where does that leave embedded types? What is the relationship between the embedded and the embeddee?
@cznic yup, that's about as clear as I can make it
@henryas
A Carpenter is a Person that build furniture. A Singer is a Person that sings" in Go value types?
A type Carpenters struct{ ... }
is-a Carpenter
nothing more, nothing less. A Carpenter cannot be assigned to any other named type in Go.
A type Person struct{ ... }
fulfils the type Singer interface{ ... }
because it has-a func (p *Person) Sing() { ... }
method.
@henryas
Where does that leave embedded types? What is the relationship between the embedded and the embeddee?
There is no relationship. type Pencil struct { ... }
has no knowledge that it has been embedded inside another type, and that embedding has no effect on its behaviour.
@henryas
The above code has no interface whatsoever, and yet the Person "has a" Pencil.
This is not correct, type Person struct { ... }
has a method called Pencil()
and a field called pencil
. As pencil
is not embedded into Person
, this is neither inheritance, nor embedding.
In Go, proper composition should look like this:
type Person struct{ pencil Pencil } func (p Person) Write (book Book) { //do something with pencil and book }
Go supports this already, at the risk of sounding sarcastic, these are just struct fields
. The value of anonymous structs
is that they propagate structure and function at the field
level whereas inheritance propagates function at the type
level. Inheritance is a crippled construct such that the structure can not be altered down the creek and the user has to deal with the contract defined in the base class.
@davecheney I am still puzzled. A Carpenter is a specialized version of A Person. A Singer is another specialized version of a Person. Each has different skills. How do you present this "is a" relationship in value type? How do you associate Carpenter to Person, and Singer to Person?
Also, the code snippet I showed wasn't about embedded type. Sorry if I wasn't clear. It was to show the "has a" relationship without resorting to interface. The question was how interface comes to play into this and whether interface is the defining feature to present a "has a" relationship?
About embedded types, it is true that the embedded does not know about the embeddee. However, the embedee is aware about the embedded. At first, it looks like a composition. However, there is a syntactic sugar feature that allows you to access embedded's behavior without being explicit that you are accessing the embedded type. In the example I gave above, person.Pencil.Manufacturer()
is clearer than person.Manufacturer()
, which is misleading.
@as Yes, Go already supports that. That is what the normal composition looks like. That's how it is done in most other languages. The proposal is not to introduce new features. The proposal is to scrap embedded types, because embedded type is not needed for composition. If embedded type is needed for inheritance, then it is something else entirely. The problem is people keep saying embedded type is not inheritance. Some say it is composition, but we can already do composition without embedded type. Now, davecheney says embedded type is neither a "has a" nor a "is a" relationship, and that there is no relationship between the embedded and the embeddee.
I am puzzled. Maybe someone can better explain to why we need embedded types?
@henryas
@davecheney I am still puzzled. A Carpenter is a specialized version of A Person. A Singer is another specialized version of a Person. Each has different skills. How do you present this "is a" relationship in value type? How do you associate Carpenter to Person, and Singer to Person?
A Carpenter
is not a specialised version of a Person
in Go because you cannot write that code. We can only talk about what is possible to write in Go, not the metaphysical.
I am puzzled. Maybe someone can better explain to why we need embedded types?
To make it pleasant to compose types that implement interfaces.
@davecheney
@cznic yup, that's about as clear as I can make it
Well, then let's look at it again. You wrote
In Go value types satisfy the "is-a" relationship, and interface types satisfy the "has-a" relationship.
Wikipedia thinks "is-a" is about inheritance/subtyping and that "has-a" is composition. Plugging into the above I get something like
In Go value types support subtyping/inheritance, and interface types support composition.
@cznic
Wikipedia thinks "is-a" is about inheritance/subtyping and that "has-a" is composition. Plugging into the above I get something like
That's why I wrote
A type Carpenters struct{ ... } is-a Carpenter nothing more, nothing less. A Carpenter cannot be assigned to any other named type in Go.
That is, there is no inheritance in Go. A Carpenter
is only a Carpenter
.
I am puzzled. Maybe someone can better explain to why we need embedded types?
It seems that theoretical discussions will not advance the topic. I would like to know how you would construct AddressBar bar without embedding *Frame. Assume all 31 methods of Frame are needed.
type AddressBar struct{
url string
*Frame
}
func (s AddressBar) String() string{ return a.url}
// Frame API - This exists in another package somewhere
func New(r image.Rectangle, ft *font.Font, b *image.RGBA, cols Color, runes ...bool) *Frame
func (f *Frame) Bounds() image.Rectangle
func (f *Frame) Close() error
func (f *Frame) Delete(p0, p1 int64) int
func (f *Frame) Dirty() bool
func (f *Frame) Dot() (p0, p1 int64)
func (f *Frame) Full() bool
func (f *Frame) Grid(pt image.Point) image.Point
func (f *Frame) IndexOf(pt image.Point) int64
func (f *Frame) Insert(s []byte, p0 int64) (wrote int)
func (f *Frame) Len() int64
func (f *Frame) Line() int
func (f *Frame) Mark()
func (f *Frame) MaxLine() int
func (f *Frame) Paint(p0, p1 image.Point, col image.Image)
func (f *Frame) PointOf(p int64) image.Point
func (f *Frame) RGBA() *image.RGBA
func (f *Frame) Recolor(pt image.Point, p0, p1 int64, cols Palette)
func (f *Frame) Redraw(pt image.Point, p0, p1 int64, issel bool)
func (f *Frame) RedrawAt(pt image.Point, text, back image.Image)
func (f *Frame) Refresh()
func (f *Frame) Reset(r image.Rectangle, b *image.RGBA, ft *font.Font)
func (f *Frame) Select(p0, p1 int64)
func (f *Frame) SetDirty(dirty bool)
func (f *Frame) SetFont(ft *font.Font)
func (f *Frame) SetOp(op draw.Op)
func (f *Frame) SetTick(style int)
func (f *Frame) Size() image.Point
func (f *Frame) Sweep(ep EventPipe, flush func())
func (f *Frame) Tick()
func (f *Frame) Untick()
@henryas Yes, it is difficult to weigh one feature against another, but when considering changes to the language that is what we must do.
Right now the language has the feature of embedded types with method promotion. Removing that feature would break, at the very least, thousands of programs. We aren't going to break thousands of programs on a whim. We need a good reason.
An example of a good reason would be a set of user experience reports (https://github.com/golang/go/wiki/ExperienceReports) from different people writing substantial Go code who ran into subtle bugs because of their use of embedded types. Another example would be an analysis of Github repos that use Go code in which people changed their program to stop embedded types, with some examination of why they made that change. Another example would be a large number of people supporting this proposal even knowing that it would break code (as I write this the reaction on the initial proposal is three thumbs up, fifteen thumbs down).
We are not going to remove a significant, widely used, feature that has been in the language since the public release without a very good reason. Intuition is not enough.
To be fair, code generation could take care of most of the features provided by type embedding.
Creating methods to forward calls to methods with the same name and signature is trivial (maintaining them as things change, less so, but doable with adequate tooling).
If that were the only thing type embedding provided, there could be an argument for removing the feature and updating code that used it would be trivial.
However, these can only be done with type embedding now:
Even if there were agreement that type embedding should be removed, updating any code that used those two features (and there is a lot of it) would be easy to detect statically but very difficult to actually replace, if possible at all.
Promoted fields would at worst be time consuming, though perhaps possible to handle by a go fix style update.
Promoted unexported methods could require re-architecting entire systems since the interfaces satisfied by the types would change in a way that would be impossible to replace.
@as It depends on how you would conceptually relate AddressBar to Frame. If AddressBar is a specialized version of Frame, then using embedded type is appropriate. It is an "is-a" relationship and the classic case of an inheritance. The Go code would be as follows:
type AddressBar struct {
*Frame
}
Actually, there is nothing wrong with inheritance. Like all features, it has its pros and cons. Major programming languages today still use it. I am just baffled by the fact that some people are in denial of the fact that you have inheritance in Go.
However, in the spirit of this proposal, I am going to show that there are other interesting ways to solve the same problem without resorting to using embedded types.
First, notice that Frame handles the visual element, while AddressBar is just some data to display. By using composition, we can do this:
type Frame struct {
content FrameContent
}
//followed by other definition of Frame..
type FrameContent interface {} //FrameContent defines the signature of AddressBar, SearchBar, etc.
//Now we can do this
frame1:= NewFrame(addressBar)
frame1.Show()
frame2:=NewFrame(searchBar)
frame2.Show()
frame3:=NewFrame(NewCompositeFrame(addressBar, searchBar))
frame3.Show()
Now, let's say Frame is already written beforehand by someone else, and it is frozen. There is no way you can make modification to it to accommodate your code. It requires security clearance level 5 and you are just the new guy with access to only your cubicle and the restroom. You need Frame and Frame is beyond your control. The simplest way is to just wrap Frame and use forwarding methods.
type AddressBar struct{
frame *Frame
}
func (a *AddressBar) Bound() image.Rectangle {
return a.frame.Bound()
}
func (a *AddressBar) Close() error {
//you may perform additional clean up for AddressBar here ..
return a.frame.Close()
}
func (a *AddressBar) Delete(p0, p1 int64) int {
return a.frame.Delete(p0, p1)
}
//... and the rest of the Frame's methods here.
//Note that you do not need to implement all Frame's 31 methods.
//Just pick the ones that are relevant to AddressBar,
//but you may implement all of them as long as they are relevant.
At first, it seems it takes some extra work to write all those forwarding methods, but it is easy and it really doesn't require that much effort (even if you have 31 methods to write). The process is mostly mechanical with barely any thinking involved. It doesn't even require much time. However, the little effort spent here is a well-spent investment. It limits the cascading effect of any change to Frame. Remember that the guy with the level 5 clearance may be a mean guy and may not even know you. If you have extremely high test coverage to lock down your code, you may be able to detect the impacts of the changes that the level 5 guy made to Frame, and fix any bugs it causes to your work without resorting to such method. However, in reality, it is often not practical to have 100% test coverage. In my own typical projects, we usually get about 55-65% test coverage. That means we have about 35-45% of movable code that may deviate from the specs without alerting us. While it won't prevent all the bugs caused by Frame, it helps localize any potential bug.
Alternatively, you can resort to procedural styles. Sometimes things that are complicated to express in terms of objects may be simpler to express in procedures.
package frame
type Framer interface{}
type FramerWithBorder interface{} //Frame with specialized behavior (eg. AddressBar, SearchBar)
func Bounds(f Framer) image.Rectangle
func Close(f Framer) error
func Delete(f Framer, p0, p1 int64) int
func Dirty(f Framer) bool
func Dot(f Framer) (p0, p1 int64)
func Full(f Framer) bool
func Grid(f Framer, pt image.Point) image.Point
func IndexOf(f Framer, pt image.Point) int64
func Insert(f Framer, s []byte, p0 int64) (wrote int)
func Len(f Framer) int64
func Line(f Framer) int
func Mark(f Framer)
func MaxLine(f Framer) int
func Paint(f Framer, p0, p1 image.Point, col image.Image)
func PointOf(f Framer, p int64) image.Point
func RGBA(f Framer) *image.RGBA
func Recolor(f Framer, pt image.Point, p0, p1 int64, cols Palette)
func Redraw(f Framer, pt image.Point, p0, p1 int64, issel bool)
func RedrawAt(f Framer, pt image.Point, text, back image.Image)
func Refresh(f Framer)
func Reset(f Framer, r image.Rectangle, b *image.RGBA, ft *font.Font)
func Select(f Framer, p0, p1 int64)
func SetDirty(f Framer, dirty bool)
func SetFont(f Framer, ft *font.Font)
func SetOp(f Framer, op draw.Op)
func SetTick(f Framer, style int)
func Size(f Framer) image.Point
func Sweep(f Framer, ep EventPipe, flush func())
func Tick(f Framer)
func Untick(f Framer)
func ShowBorder(f FramerWithBorder)
func HideBorder(f FramerWithBorder)
The benefit of this approach is that each type and each function exist independently. The change in one does not impact the others. The given example may not be the best example for this scenario, but I have seen this pattern done to solve a fairly complex OO structure into an elegant solution. It brings down the complex type hierarchy into flat independent components that can be plugged into one another.
However, when you have 31 methods, it may be time to examine whether you really need all of them. LOL.
@ianlancetaylor If you are concerned about breaking changes, my suggestion is to just ignore this proposal and leave embedded type as it is. As mentioned, the proposal is about embedded type being an extraneous feature. There is really no harm in leaving it as it is. If people don't like it, they can just not use it.
However, if the Go Team is looking for potential candidates to trim down the language, embedded type should be considered. That's my proposal.
@davecheney I still don't understand what you are talking about. In my limited experience, when you start hearing people speaking like Master Yoda, it's time to call it a day and wait till you sober up. Perhaps I should re-read your posts again in the morning.
I am just baffled by the fact that some people are in denial of the fact that you have inheritance in Go.
Formal definitions are important because they allow opinions to exist separately from fact. Disagreeing with the common knowledge that Go does not support inheritance
and even being baffled by it is well within your right, but it only sets your proposal back.
At first, it seems it takes some extra work to write all those forwarding methods, but it is easy and it really doesn't require that much effort (even if you have 31 methods to write). The process is mostly mechanical with barely any thinking involved. It doesn't even require much time.
I notice only 3-4 were explicitly written out, and only the signatures. My example effectively summarized all 31 methods. This seems to contradict some assertions that embedding is confusing and suggests that embedding is a concise and elegant way of expressing structure even outside of the actual codebase.
However, when you have 31 methods, it may be time to examine whether you really need all of them. LOL.
31 is a large number for playground examples, not so much for production code. Laughing out loud doesn't help the number go down either. The requirement is that you need the 31 methods, writing a comment saying you don't need them isn't a valid solution, and would likely lead to undesirable outcomes.
I will say that in the 6 years I've used Go, not once have I ever seen a bug that resulted from an embedded type. Do you have any concrete examples of the contrary?
Do you have any concrete examples of [bugs due to embedded types]?
I don't support this proposal, but In the interest of fairness I do have such an example. The interface proto.Message
is a tag-interface that represents “types generated by the protobuf compiler”. If a type that implements proto.Message
is embedded in a struct, that struct will appear to also implement proto.Message
, but may cause a panic if passed to a function that expects only generated message types.
(That's https://github.com/golang/protobuf/issues/364, and it's arguably more a misuse of interfaces than a defect of embedding proper.)
@davecheney, you are using is-a and has-a in the opposite of their usual senses in a programming context. You may be doing so intentionally to make a point, but it comes across as either confused or confusing.
Go interfaces have the usual OOP is-a relationship if you map the OOP is-a concept to its closest Go analogue, is-assignable-to. In that sense, a net.Error
is-a error
, because the method set of net.Error
is a superset of the method set of error
.
In that sense, interfaces embedded into structs do somewhat blend is-a and has-a: a struct that embeds a sync.Locker
, for example, both is-a sync.Locker
(because it has the methods required of a sync.Locker
) and has-a sync.Locker
(addressable via the Locker
field).
@henryas When we say that “Go does not have inheritance”, what we generally mean is that Go does not have dynamic dispatch: you can forward the methods of a type by embedding, but you cannot “override” its methods. Within the methods of the embedded type, all (recursive) method calls will go to the definitions in the embedded type, not the redefinitions of those methods in the embedding type.
https://github.com/golang/go/issues/18617 describes another problem with embedding-as-composition (with https://github.com/golang/protobuf/issues/276 as its concrete example). Namely, embedding has a surprising (and often frustrating) interaction with nil
receivers. However, I believe it would be easier to remedy that problem by redefining the behavior of embedded methods with nil
receivers, rather than by eliminating embedding entirely.
@henryas
The problem with automatic method implementation is that the people […] may inadvertently break the original assumption about the [embedding types].
If you embed one Go type in another, you must not assume the non-existence of methods on the embedded type. This expectation is clearly documented in the Go 1 compatibility policy.
In other words: if you break a user's assumptions by adding a method, the user's assumptions were wrong. The two proto
examples I mentioned above notwithstanding (as both are due to abuse of interfaces in the proto
package), I am not aware of widespread patterns of errors of that sort.
@bcmills
I am not in favor of this proposal either, but ever since mentioning that I've not seen a problem with embedding-as-composition I have been finding issues about it. Probably due to an increased awareness that they exist.
Nice example. The same issue comes up fairly often with fmt.Stringer
and other magic-method interfaces (see also #4146).
Do you have any concrete examples of [bugs due to embedded types]?
Here's a bug from Go's flavor of the App Engine Datastore. I can't remember the exact details, but it's along these lines...
Briefly, when serializing, Go structs' field names are flattened so that if you have:
type Foo struct {
B Bar
}
type Bar struct {
X int
}
and you serialize a Foo, you'll get a "B.X" field.
You can embed the Bar:
type Foo struct {
Bar
}
type Bar struct {
X int
}
Importantly, the flattened field name is "X", not "Bar.X". Doing it this way affects the queries you'd write (in GQL) and seemed consistent with how you'd write actual, idiomatic Go code: "foo.X = etc".
In hindsight, we might have done it differently, but we didn't have hindsight then, and once you store data in production, you can't change the underlying (language-agnostic) database's field names lightly.
Separately, the Go App Engine Datastore also lets you store a time.Time value. This has special semantics, so you can query the datastore by time (the concept), not just time-as-a-structure-with-integers. If you had
type Qux struct {
T time.Time
}
Then it's serialized as a single field (called "T"), not multiple fields ("T.wall", "T.ext", etc), one for each member of the time.Time struct. In hindsight, this assumes that that Go field has a name (e.g. "T").
We had a bug because these two features mix: it's not recommended (and the interaction wasn't forseen at design time), but you can indeed embed a time.Time inside the top-level Foo struct that you're serializing:
type Foo struct {
time.Time
}
The question is how to flatten such a Foo to a wire format that the server accepts. Remember that for the earlier example, the flattened field name is "X", not "Bar.X". Thus, the field names here should be something like "X" and not "Time.X".
On the other hand, a time.Time struct is a struct that's treated specially, so we don't look at the "X" fields of the struct. Besides, such fields are unexported (and change over Go revisions, with the addition of monotonic time). So there is no "X".
There's no "Time" and no "X", so the flattened field name is the empty string "". The server didn't like that.
This bug was due to an interaction with embedding and something else, not just with embedding per se, but to me, it reinforces @griesemer's earlier quote:
Without casting any judgement on this proposal, I note that embedding is also a significant cause of complexity in the language and implementation.
Not just for language and implementation, but I suspect it also causes complexity for serialization libraries such as JSON and XML.
As for casting judgement, I've been bitten enough times (once is enough!) that I'd consider removing embedding, if we were starting again from scratch. As for Go 2.0, I haven't been following such discussions closely, but there might be a bigger concern of incrementally migrating Go 1 to Go 2, that trumps being able to simply remove embedding. I don't know.
OTOH, I do define a number of FooEmbed types in https://godoc.org/golang.org/x/exp/shiny/widget/node and as their name suggests, they are designed specifically for embedding into other structs. Values of such structs form a tree of GUI widget nodes. Those nodes don't all have to have the same concrete type, but I still wanted to share some common fields, such as the parent/child links.
These FooEmbed types are just a convenience, of course, as it would be straightforward for the embedders to explicitly rather than implicitly forward those methods on. Straightforward and mechanical, but certainly less convenient. Less magical too, in both the good and bad senses.
These FooEmbed types are just a convenience, of course, as it would be straightforward for the embedders to explicitly rather than implicitly forward those methods on.
That isn't true of every embedded type, though. For example, the strategy that I proposed in https://github.com/golang/protobuf/issues/276 for removing the (awkward and distracting) XXX_
fields from generated Protocol Buffer types makes use of unexported methods on the embedded type, which embedders cannot forward explicitly.
That isn't true of every embedded type
Ah, I forgot about that. I suppose that that just reinforces @griesemer's point that embedding leads to complexity.
It seems the method promotion is being confused for inheritance just because we can dispatch methods using the instance of type that embeds other types. My understanding of the whole concept might be wrong but I think the following example clarifies the difference.
package main
import (
"fmt"
)
type Y struct {
Value string
}
func (y Y) GetY() string {
return y.Value
}
type X struct {
*Y
Value string
}
func (x X) GetX() string {
return x.Value
}
func main() {
x := X{Y: &Y{Value: "Y"}, Value: "X"}
fmt.Println(x.GetX)
fmt.Println(x.Value)
fmt.Println(x.GetY())
fmt.Println(x.Y.Value)
}
would result in
X
X
Y
Y
Here X embeds Y but X.y is still an instance of type Y. X is not adopting Y's behavior and creating 1 final instance that represents both X and Y. X only creates an instance of itself while holding onto a reference to another instance of Y.
Consider these 2 rough python examples:
Inheritance:
class X:
value = 1
class Y(X):
value = 2
vs composition
class X:
value = 1
class Y;
x = X()
value = 2
In the second python example, we would have to define pass-through methods on Y if we wanted to call methods on Y.x directly from an instance of Y. If python did this automatically for us, would we consider it inheritance? It would still be composition but with some convenience. That is exactly what Go does.
So I guess the question is whether method promotion is useful enough or not in Go. I think it is quite useful and in no way adds inheritance to Go.
X is still an instance of type Y
This is not correct. An X cannot be substituted for a Y in Go, with the exception of if Y is an interface and X implements Y’s method set.
On 11 Nov 2017, at 15:32, Owais Lone notifications@github.com wrote:
X is still an instance of type Y
Ignore me, I quoted too narrowly and missed the context of your point. My apologies.
On 11 Nov 2017, at 15:37, Dave Cheney dave@cheney.net wrote:
X is still an instance of type Y
This is not correct. An X cannot be substituted for a Y in Go, with the exception of if Y is an interface and X implements Y’s method set.
On 11 Nov 2017, at 15:32, Owais Lone notifications@github.com wrote:
X is still an instance of type Y
Not your fault. It is a badly written sentence :)
@henryas
To avoid the "base class extension breaks the derived class" issue: Maybe it would be enough to forbid embedding of types that are defined inside other packages? Combine this with a godoc change to show methods of embedded types as special marked methods of the derived type for visibility.
I would like to propose the removal of embedded struct in Go 2.x, because it does not solve inheritance problems, and the alternatives may actually be better.
First, let us examine the pros and cons of inheritance (more specifically, implementation inheritance). Inheritance groups common functionalities into the base class. It allows us to make a change in one place (instead of being scattered everywhere), and it will be automatically propagated to the derived classes. However, it also means the derived classes will be highly coupled to the base classes. The base class implementor, who does not need to know anything about the derived classes, may inadvertently break the derived classes when making changes to the base class. In addition, inheritance encourages a type hierarchy. In order to understand a derived class, one need to know its base classes (which includes its immediate parents, its grandparents, its great grandparents, etc.). It gives us the classic OOP problem where in order to get a banana, you need the monkey and the whole jungle. Modern OOP proponents now encourage "composition over inheritance".
Go's embedded struct is unique. At a glance, it appears to be a composition. However, it behaves exactly like the classic inheritance, including the pros and cons of inheritance. The base struct implementor may inadvertently break the derived structs. In order to understand a derived struct, one must look up the definition of its base struct, creating a hierarchy of types. Thus, Go's embedded struct is actually inheritance (although it is a crippled one).
I say it is a crippled inheritance because Go's embedded struct does not provide the same flexibility as the actual inheritance. One such example is this:
driver.Drive(s60.Volvo.Car.Vehicle)
Even though s60 is a vehicle, in Go, you need to get to the actual base class in order to use it as an argument.
If the Go's Team considers keeping this inheritance property, I say they should either go all the way with inheritance, or scrap it altogether. My proposal is to scrap it.
Here are my reasons for scrapping it:
You can do this
Let me know what you think.
Thanks.
Henry