I’m excited to share a new package I’ve been working on: quantify ! It’s a type-safe units of measurement library for Dart, designed to make working with physical units safer, more readable, and efficient.
Motivation & Goals:
-
True Type Safety: I wanted to catch unit mismatch errors at compile-time, not runtime. No more accidentally adding meters to seconds!
-
Intuitive API: This project was also a bit of an API proof of concept for me. I really wanted to explore Dart’s extension methods to create a fluent and natural syntax. Think 10.m (for meters) or myLength.inKm. It was a fun experiment that I think paid off!
-
Performance: Unit conversions should be fast. quantify uses double for precision and ensures that most conversions are just a single multiplication, thanks to pre-calculated factors.
-
Operator Overloading: Performing arithmetic with quantities should feel natural, like distanceA + distanceB.
-
Easy String Representation: Getting a nicely formatted string with the value and unit symbol (e.g., “1.5 km”) should be straightforward and configurable.
What quantify offers (v0.1.0):
Since this initial version focuses on establishing the core API and type safety, the number of units is currently limited. Many more are planned for future releases
-
Type-Safe Quantities: Classes like Length, Time, Temperature, and Pressure ensure you’re working with the correct types.
-
Elegant Syntax:
final pathA = 1500.m; // Create with ease using short extensions
final pathB = 2.5.km;
double pathAInMiles = pathA.inMi; // Get value in another unit
Length pathBAsYards = pathB.asYd; // Get a new Quantity object
-
Immutable: Quantity objects are immutable for safer code.
-
Highly Configurable toString():
print(pathA.toString(targetUnit: LengthUnit.kilometer, fractionDigits: 1));
// Output: "1.5 km"
// With non-breaking space and locale support (if you use 'intl')
print(pathA.toString(
targetUnit: LengthUnit.yard,
unitSymbolSeparator: '\u00A0', // Non-breaking space
locale: 'de_DE', // For German number formatting
fractionDigits: 0,
));
// Output: "1640 yd" (approx, German locale might show "1.640 yd" if grouping is active)
Arithmetic Operations:
final totalDistance = pathA + pathB; // pathB is auto-converted
print(totalDistance.toString(fractionDigits: 0)); // "4000 m"
- ↔️ Comparisons & Sorting: Quantities are Comparable.
Get Started:
-
Pub.dev: https://pub.dev/packages/quantify
-
GitHub Repository: https://github.com/PhilippHGerber/quantify
As this is an early version and an API exploration, your feedback is especially valuable. Bug reports, or feature requests (especially for new units!) are highly welcome. Please open an issue or a PR on GitHub.
Happy coding!
Philipp