News for hackers. Stuff that matters.
Re:Value-Oriented Programming by nexys on Friday August 31, 2018 @01:49PM (#17887532)
What is the difference between subtyping and subclassing? It looks confusing to me.
Re:Value-Oriented Programming by naasking on Friday August 31, 2018 @04:46PM (#17888527)
Subtyping describes an abstraction with which you can substitute any implementation that conforms to that abstraction's signature (Liskov substitution). Subclassing instead typically imposes both subtyping and behavioural inheritance. So as a rough approximation:

Interface implementation = subtyping

Class inheritance = subclassing + subtyping, ie. inheriting behaviour + subtyping

An OO language where you were forced to specify subtyping by implementing interfaces, but you could separately declare some implementations you inherited from without that also applying a subtyping relationship with those types, then that would be a proper separation of concerns.

Oleg discusses the issues in further detail here: http://okmij.org/ftp/Computation/Subtyping/

Re:Value-Oriented Programming by zwieback on Friday August 31, 2018 @02:29PM (#17887794)
Oversimplifying for this context: subclassing is reusing the implementation of a parent class, subtyping is when a type can be substituted for a (parent) class for some given functionality. It's confusing because it's not always easy to explain what constitutes "substitute", e.g. see "Liskov Substitution Principle". In some languages there might not be any class or interface relationship between subclasses - they just have the right functions that do the right things.
Re:Value-Oriented Programming by kroltan on Friday August 31, 2018 @02:48PM (#17887891)
As I understand it, subtyping is the actual conflation of interfaces (not necessarily a syntatical `interface`, but in the logical sense of "exposed things") into one type, and subclassing is direct extension, the so-called "is a" constraint.

For example, in Rust, there is no subclassing: There is no concrete type that is also another type. But there is subtyping, that is, a concrete type can be used where a interface (in Rust, called `trait`) is required, granted it implements that interface. Additionally, external sources can implement their interfaces unto existing external types.

In C#, you have subtyping+subclassing, but only as one. That is, you can have a concrete type B that is an A, and if A implements X, then B implements X. But you can't implement Y for A unless you have access to A's source. So if you want a type that is an A but also does Y, you _have_ to subclass A as a B, and implement Y on B.

Re:Value-Oriented Programming by steveklabnik on Friday August 31, 2018 @04:04PM (#17888294)
The only subtyping in Rust, in my understanding, is lifetimes with the : syntax. Traits are not subtypes.
Re:Value-Oriented Programming by lmm on Friday August 31, 2018 @04:28PM (#17888428)
What's the distinction you're drawing? Certainly with "impl trait", any concrete type that conforms to "Iterator<Item = i32>" seems to be a subtype of "impl Iterator<Item = i32>" in every meaningful sense (e.g. Liskov substitutability).
Re:Value-Oriented Programming by steveklabnik on Friday August 31, 2018 @04:55PM (#17888573)
I’m not drawing a distinction, I’m repeating what those who know more about types than me say. Maybe those people are wrong, but if so our docs need updated!

(I think it’s because traits aren’t types, and therefore can’t be subtypes. But I’m not 100% sure and my copy of TAPL is thousands of miles away at the moment...)

Re:Value-Oriented Programming by kroltan on Saturday September 01, 2018 @12:13PM (#17892490)
Ok, I guess, this only works with trait objects or generics, so traits themselves aren't types in the formal sense (as suggested by others. I am not a type system expert nor do I have formal CS theory education).

As I understand (which again, might be flawed, please correct me), it would be more better to say that a type parameter bounded by a trait is a type, and a type implementing that trait is a subtype of the parameter?

But trait objects have types, and such can be subtyped, correct? That is, I can use a `&Bar` where a `&TraitFoo` is expected if `impl TraitFoo for Bar`.

Re:Value-Oriented Programming by lmm on Saturday September 01, 2018 @07:16PM (#17894411)
I think the point is that `&TraitFoo` would never be expected - that there was no way to have that be the type of a term in the first place? I don't think that can reasonably be said to be true in the days of impl trait though.
Re:Value-Oriented Programming by naasking on Friday August 31, 2018 @04:43PM (#17888513)
Traits are subtyped, they are simply a restricted kind of value that makes them more tractable than general value subtyping.
Re:Value-Oriented Programming by taeric on Friday August 31, 2018 @04:07PM (#17888313)
Perhaps an example? List<String> is a subtype of List, ArrayList is a subclass?

Question marks, because I am not 100% that I have it correct, either. Hoping someone will correct. :)