17
votes
Are there any practical use cases for subtyping primitive types?
Yes. One application of subtyping is to produce an object that behaves the same way as another object, but records the operations performed on it. This recording can be used in a number of ways, e.g....
13
votes
Are there any practical use cases for subtyping primitive types?
Refinement Types
A Refinement Type is a subtype of some type, whose inhabitants can be described by a predicate.
It looks like this: Even = { n ∈ ℤ | n mod 2 = 0 }.
...
11
votes
Are there any practical use cases for subtyping primitive types?
User-defined subtypes of primitive types are a thorny issue, because primitive types have many operations which return new values of the primitive type. For example, if you add two of your ...
9
votes
Accepted
What options are there for code sharing/default implementations in a structurally-typed language?
I think this is equating nominal subtyping with inheritance too strongly, when code reuse and subtyping are really orthogonal axes that can be bundled together (as in C# class inheritance), but could ...
5
votes
Are there any practical use cases for subtyping primitive types?
Marking encodings (safe vs. unsafe strings)
Like Occipita, I only really see a use for strings.
In Django and Jinja, there is a subtype of string called "SafeString". It has no actual custom ...
4
votes
Are there any practical use cases for subtyping primitive types?
I think you should first ask whether there are practical use cases for subclassing at all.
There is nothing doable with subclassing that can't be done with containment and delegation (and interfaces ...
3
votes
Are there actual languages using fat pointers to store types?
Swift protocol existentials
When you use a "protocol existential" type like any CustomDebugStringConvertible, it stores the vtable alongside the value ...
2
votes
Are there any practical use cases for subtyping primitive types?
Two general use cases not specific to any particular language:
You can introduce literal types as subtypes of their respective bases.
Also, you can have enum types be regular subtypes of their ...
2
votes
How should I represent objects under prototype-based inheritance?
I think a chain of hash-maps works fairly well.
You can optimise a little bit by pre-resolving the string lookups to indexes in the map. The Wren language's VM does this. Every object gets allocated a ...
2
votes
Are there any practical use cases for subtyping primitive types?
Everything is an object
Python doesn't distinguish between primitive and object types the same way that C++ or Java does. Everything, including integers, is an object. Therefore, there's no reason why ...
2
votes
Are there any practical use cases for subtyping primitive types?
Math performance ...
Numbers are usually primitive types, and some languages go to extreme lengths to make them as fast as possible, mapping some types not as heap objects, but as byte payloads that ...
1
vote
Are there any practical use cases for subtyping primitive types?
Measurement units: meters, seconds, dollars, the like.
It may be useful to know that two values are not logically compatible, even if they binary representations are. For example, meters and seconds ...
1
vote
Are there any practical use cases for subtyping primitive types?
Subtyping of value types (whether primitive or otherwise) could be very useful in contexts involving C#/.NET-style generics. In general, references to objects are covariant (e.g. a reference to a Cat ...
1
vote
How could a language support adding new types to the middle of the inheritance tree in an ABI-stable way?
I have recently had an idea. For the purpose of this explanation, let's consider a simpler set of protocols, and a type that adopts both:
...
1
vote
How should I represent objects under prototype-based inheritance?
I think you conflate two distinct notions in your question: prototype-based inheritance and dynamic objects that can change their shape during program execution.
Prototype-based inheritance itself can ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
inheritance × 5object-oriented × 2
subtypes × 2
types × 1
memory-management × 1
pointers × 1
traits × 1
structural-types × 1
overloading × 1
swift × 1
abi-stability × 1