The standard way that types are handled in programming languages that have such a concept, is that they are 1: removed entirely at compile time and are just used to determine memory layout, function pointers etc... 2: left in some form as a sort of tag on a structure in memory, so that you can check if B instanceof A and similar, 3: types don't really exist beyond an objects properties matching at runtime what the user expects them to.
- removed entirely at compile time and are just used to determine memory layout, function pointers etc...
- left in some form as a sort of tag on a structure in memory, so that you can check if
B instanceof Aand similarly, - types don't really exist beyond an objects properties matching at runtime what the user expects them to.
C/++ would be more or less the first, C# and Java i think would be the second, because they both have some degree of reflection and can tell what their type is, and Javascript would be a mix of 2 and 3 - most people only care about properties usually, but you can compare prototype chains to see if one inherits another
One thing I haven't seen - and im not sure if it is because no languages do it or i just haven't been able to use the right search terms - is using a type as a value that can be passed around, allowing it to instantiate new objects, call static functions etc, while still providing all the benefits of strong type checking
For example, I have 2 classes, J and K, both of which implement interface I and in some circumstances may be used in the same place. Interfaces can only specify functions/properties that will exist on an instantiated object typically, but if the type itself can be passed I could have a function that takes a type implementing II and call whichever version of a static member function, based on the passed object
I could then, later, use that passed type to instantiate an object of type J or K but with the visible type of I (as expected)
The closest I have seen is javascript allowing you to pass around class constructors because classes in javascript are mostly just functions with extra steps (if my understanding is correct), and even typescript will only let you treat them as Function type and not as an specific type with a specific signature, meaning you have no type safety whilst doing so.
My reason for wanting this, or atleast wondering, is related to inversion of control containers - if I could use a type as a value, i could map a property on an object to a specific type at runtime and have direct access to the static methods and constructor for that class and not need to construct other proxy objects or factories to handle those for me