How to have strongly typed numerical values?

Nicholas Londey londey at gmail.com
Thu Sep 6 02:30:11 PDT 2012


Thank you everyone for your replies.

Initially I think I am going to role my own as my requirements 
are fairly simple compared to a full blown system with units of 
measure. My goal is to have large number of application specific 
types which may be implemented as the same but are semantically 
different.

A real world case in which I have used this approach was when 
cleaning up a large body of code using a mixture of time units 
(sec,msec,µsec) stored as 32 and 64bit signed and unsigned 
integers. We needed to standardized on µsec and explicit typing 
greatly simplified the migration process.

I have also worked on protects in the past where we speculated on 
the benefits of using strong typing to avoid incorrect mixing of 
matrices (world, model, bone etc.). I think there is allot of 
value to be had in simply requiring explicit casting however I am 
less convinced on systems that allow implicit combining of units 
of the same quantity type. I feel part of the type is its range 
and precision and so there is no valid way to implicitly add 
kilometers to millimeters for example.

I hope that at some point support for the semantically different 
versions of the same type will be added to the standard library 
but for now I am happy to write my own. D is still a new language 
to me and it has some vary nice features to help accomplish this.



More information about the Digitalmars-d-learn mailing list