3 ms·
The section "Inheriting from structs" is misleading. It's embedding, not inheriting. A big different is that it does not lead to type compatibility. Given this:
by lobster_johnson 10y ago
The section "Inheriting from structs" is misleading. It's embedding, not inheriting. A big different is that it does not lead to type compatibility. Given this:
type Foo struct {
...
}
type Bar struct {
Foo
}
...You can't pass Bar (or pointer to Bar) to anything that requires Foo (or pointer to Foo), even it looks like they'd have identical memory layouts and therefore be compatible, though that is exactly what inheritance aims to achieve in other OO languages.
- timtadh 10y agoGood point and definitely an annoying limitation. The work around is to use an interface as the expected type as I do in the lexing example. I should have included a section on the limitations of embedding. This is what happens when I don't stew on the writing for a day or two before putting it out there! I will have to make a pass through the article and clear up the terminology. Thanks for the feedback!