1
Vote

Add Units/Quantities Library

description

'Unit' struct with operators overloaded, define unit during construction and keeps track of a value's unit as operations are done. Immutable.
 
Unit distance = new Unit<int>(5, "m'); // can't generic types be inferred and so not require angle-brackets?
Unit force = new Unit<float>(3, "N");
Unit power = force * distance;
 
Console.WriteLine( power );
// 15 Nm
// 15 J // to convert from Nm to J would require a knowledge of SI units
 
I don't think "Unit" is a good name for the struct (as it's a quantity, not a unit) but you get the general idea
 
Instead of strings to denote units, maybe a bunch of classes using some trickery with implicit conversions can be used. Supporting only SI makes things simpler, but it'd be a rewarding challenge to get something that works with both SI, Imperial, US, and other systems of units. (Then again, should I be encouraging the use of antiquated unit systems?)

comments