I'm working on system development right now, when it comes to many conversions between semantically different values that have the same primitive .NET type (double / string / int). This means that you can get confused about which "semantic type" you use, either without converting or converting too many times. Ideally, I would like the compiler to give a warning / error if I try to use a value in which it semantically makes no sense.
Some examples to indicate what I mean:
- Angles can be in degrees or radians, but both are represented by
double . - Vector positions can be in local / global coordinates, but both of them are represented by the
Vector3D structure. - Imagine a SQL library that accepts various query parameters as strings. It would be nice to have a way of ensuring that only clean lines are allowed at run time, and the only way to get a clean line was to go through some SQL attack prevention logic.
I believe that F # has a compilation solution for this (called units.) I would like to do something similar in C #, although I do not need dimensional analysis, which offers units in F #.
I believe that C ++ can achieve this using typedef (although I am not an expert in C ++).
The obvious solution is to wrap double / string / whatever in the new type to provide it with the type information needed by the compiler. I am curious if anyone has an alternative solution. If you think packaging is the only / best way, then please go to some of the drawbacks of the template (and any changes that I haven't mentioned as well.) I'm particularly concerned about the performance of abstract primitive numeric types on my run-time calculations , therefore, any solution I came up with should be easy both in terms of memory allocation and sending a call.
Drew noakes
source share