6 ms·
I offer this thread as evidence: https://news.ycombinator.com/item?id=12342583 https://news.ycombinator.com/item?id=12342583
by catnaroek 10y ago
I offer this thread as evidence: https://news.ycombinator.com/item?id=12342583 https://news.ycombinator.com/item?id=12342583
- naasking 10y agoThat's definitely not being charitable. Subtyping has some pretty subtle interaction with other typing features, which is why it's still being studied more than 20 years after Cardelli's "Subtyping Recursive Types". The pernicious conflation of subtyping and subclassing in common OO programming languages just compounds the confusion.
- eli_gottlieb 10y agoHey, it's not the fault of the programming masses that some evil motherfucker conflated implementation extension (inheritance) and subtyping. Subtyping = "A <: B, therefore I can start with an A, forget the details irrelevant to B, and just use it as a B." Inheritance = "A inherits B, if I try to actually use it as a B, it promises to work. However, it might actually blow up in my face, because it was keeping its extra details secret from me and promising they wouldn't blow up."
- catnaroek 10y ago> programming masses, some evil (bleep) Too harsh. Logic is hard for humans. Evolution has trained us to take lots of mental shortcuts, not to be perfect calculating machines. Even the most technically competent people rely on their intuition. What differentiates them from the rest of us is that they have trained their intuition to make it more reliable [0]. It's the job of a language designer to arrange things so that intuition doesn't mislead programmers regarding the meaning of language features. Before proposing a new feature, the designer must anticipate how programmers will think about code that uses this feature, and there better not be a mismatch between what programmers think the code means and what the code actually means. > A inherits B, if I try to actually use it as a B, it promises to work. There's nothing wrong with implementation inheritance per se [1]. It addresses a very common use case: you have a thing Foo, and you want to produce another thing Bar that's similar to Foo, with minor changes here and there. This describes accurately how many things work in real life. However, you can't assume that Bar can always be used where Foo is expected. Those “minor changes here and there” might have rendered Bar incompatible with Foo. Hence, “inheritance is not subtyping”. As stated above, it's the job of a language designer to anticipate this. [0] https://terrytao.wordpress.com/career-advice/there%E2%80%99s-more-to-mathematics-than-rigour-and-proofs/ https://terrytao.wordpress.com/career-advice/there%E2%80%99s... [1] Other than the lack of mathematical elegance, but few things are mathematically elegant in life.
- eli_gottlieb 10y ago>Hence, “inheritance is not subtyping”. As stated above, it's the job of a language designer to anticipate this. Yes, it is their job, by not allowing subclasses to be used as subtypes.
- catnaroek 10y agoSome subclasses can be safely used as subtypes. For example, those that meet the following two conditions: (0) Not directly mutating superclass fields. (Reading them is fine.) (1) Not overriding non-abstract methods of the superclass.
- dragonwriter 10y agoIf classes are types, subsclasses should be subtypes -- so the above restrictions should just be applied to subclass relationships. One could also have an non-subclassing mechanism of implementation sharing, that might be declared something like this: class Related does (method1, method2) like Base; or class Related does all except (method3) like Base;
- catnaroek 10y agoThat syntax is too heavyweight (IMO). AFAICT, most of the time, when people use inheritance, they don't want to define perfect subtypes. They just want to reuse as much already implemented behavior as possible.
- dragonwriter 10y ago> That syntax is too heavyweight (IMO). I'd be fine with something more compact for the default case of "steal all behavior except what is explicitly overridden", such as: class Related like Base;
- catnaroek 10y ago