4 ms·
Type system for primitive values that carries meaning. Meters shouldn't be added to feet, ID of one entity can't be used as ID of another etc. I know some lang
by Ginden 5y ago
Type system for primitive values that carries meaning. Meters shouldn't be added to feet, ID of one entity can't be used as ID of another etc.
I know some languages implement such features, but these aren't mainstream ones.
- btown 5y agoThis would be tremendously powerful if primitive “colors” automatically label (or poison) any data structures they are placed into, and one can guard against certain colors being used in specific places. And casting to override this requires specific keywords that can be caught in audits. For instance, a password can never leak to logging or a client response. An unescaped user input can never leak to a client response unless explicitly casted - and user provided data from a database counts this way too!
- TheMonarchist 5y agoThere are multiple C++ libraries for just that. Search "c++ units".
- Ginden 5y agoYou can build such library in any language with operator overloading, but the point is, it's optional library. It isn't built into language, therefore randomly picked library just won't support it.
- TheMonarchist 5y agoYou can submit a paper to ISO WG21 concerning adoption of your favorite units library, but it won't ever be baked into the language, because it can be done as a library. Meanwhile a randomly picked library can either roll its own solution or pick a suitable one. Presumably Boost Units is all but standard.
- detaro 5y agoThird option: not use anything like that, because own solution is ugly and people dislike "unneeded" dependencies, especially Boost has a very split reputation.