Towel provides type-safe measurement types (e.g., Length<T>, Mass<T>, Speed<T>) to prevent mathematically invalid operations and automate unit conversions.
Automatic Unit Conversion
When performing operations on measurements of the same type, conversions are handled automatically based on the units used.
Angle<double> angle1 = (90d, Degrees);
Angle<double> angle2 = (.5d, Turns);
Angle<double> result1 = angle1 + angle2; // Result is 270°
Type Safety
You cannot perform operations between incompatible measurement types (e.g., adding Length<T> to Angle<T>) at compile time.
Manual Conversion and Parsing
- Index Operator:
double radians = angle1[Radians]; - Static Convert:
Angle<double>.Convert(7d, Radians, Degrees); - Parsing:
Speed<float>.TryParse("20.5 Meters / Seconds", out var speed);
Integration with Vectors
Measurements can be used as the generic type within Vector<T>.
Vector<Speed<float>> velocity1 = new Vector<Speed<float>>((1f, Meters / Seconds), ...);
Vector<Speed<float>> velocity2 = new Vector<Speed<float>>((1f, Centimeters / Seconds), ...);
Vector<Speed<float>> velocity3 = velocity1 + velocity2; // Automatic conversion applied
// Automatic Unit Conversion
Angle<double> angle1 = (90d, Degrees);
Angle<double> angle2 = (.5d, Turns);
Angle<double> result1 = angle1 + angle2; // 270°
// Type Safeness
Length<double> length1 = (2d, Yards);
// object result2 = angle1 + length1; // WILL NOT COMPILE!!!
// Manual Unit Conversions
double angle1_inRadians = angle1[Radians];
double angle3 = Angle<double>.Convert(7d, Radians, Degrees);
// Measurement Parsing
Speed<float>.TryParse("20.5 Meters / Seconds", out Speed<float> parsedSpeed);