Skip to main content
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....
occipita's user avatar
  • 599
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 }. ...
Longinus's user avatar
  • 1,913
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 ...
kaya3's user avatar
  • 22.4k
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 ...
Michael Homer's user avatar
  • 15.3k
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 ...
mousetail's user avatar
  • 9,559
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 ...
benrg's user avatar
  • 546
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 ...
Bbrk24's user avatar
  • 9,672
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 ...
feldentm's user avatar
  • 2,418
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 ...
springogeek's user avatar
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 ...
Adamátor's user avatar
  • 1,095
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 ...
André L F S Bacci's user avatar
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 ...
Audrius Meškauskas's user avatar
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 ...
supercat's user avatar
  • 2,312
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: ...
Bbrk24's user avatar
  • 9,672
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 ...
Alex Chichigin's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible