This is getting increasingly off-topic, but:

On Fri, Jan 1, 2021 at 7:37 AM Alex Besogonov <alex.besogo...@gmail.com>
wrote:

> Yes, it does make it usable. See: https://play.golang.org/p/IqxpujkqXnO
>

I don't understand the definition of subtyping you are using. AIUI, you
would also argue that `Base` is a subtype of `int`, because you can write
`&b.val` to get an `*int` and thus "use a `Base` as an `int`", correct?
Because if so, I don't think that's a very common understanding of the word
subtype.

There’s simply no automatic upcasting.
>

 The definition of subtyping I tend to rely on is "S is a subtype of T, if
you can safely substitute a term of type T with a term of type S" (the
Liskov substitution principle). That is, in the example, you would have to
be able to replace `Base{}` (which is a term of type Base) with `Derived{}`
(which is a term of type Derived). Clearly, that's not the case:
https://play.golang.org/p/JnAMIUPNX82

It *is* the case (though that's not necessarily easy to see) if S is
assignable <https://golang.org/ref/spec#Assignability> to T - which among
other things, is the case if T is an interface type and S implements T.

FWIW, after looking at this section of Wikipedia
<https://en.wikipedia.org/wiki/Subtyping#Relationship_with_inheritance> I
can say I was wrong to equate the terms "inheritance" and "subtyping",
though. The article about inheritance
<https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)>
doesn't contain a definition I feel comfortable arguing about, so I don't
want to really make an argument about Go having or not having inheritance.
I would say, though, that this sentence seems to strongly imply that
struct-embedding also does not constitute inheritance (and it reflects what
I would call a commonly held opinion in the Go community):

Inheritance is contrasted with object composition
> <https://en.wikipedia.org/wiki/Object_composition>, where one object
> contains another object (or objects of one class contain objects of another
> class); see composition over inheritance
> <https://en.wikipedia.org/wiki/Composition_over_inheritance>. Composition
> implements a has-a <https://en.wikipedia.org/wiki/Has-a> relationship, in
> contrast to the is-a relationship of subtyping.


(funnily enough, this sentence also implies that inheritance and subtyping
are the same, even though the sentence before that calls out that they
aren't. It's sometimes quite frustrating arguing about computer science,
where apparently no one really agrees what the definitions *are*, that we
argue about…)

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG6QR50FLAp4UF4t8hq2w8_2_79oQXcfahXtjZAa-HP7A%40mail.gmail.com.

Reply via email to