Multiplication/division of two unit instances, yielding a unit instance representing the product/quotient unit.
Multiplication/division of an unit and a value type, constructing a Quantity instance.
struct Inch { mixin UnitImpl; static string toString(UnitString type = UnitString.name) { final switch (type) { case UnitString.name: return "inch"; case UnitString.symbol: return "in"; } } alias AliasSeq!( Conversion!(centi(metre), toCm, fromCm) ) Conversions; static V toCm(V)(V v) { return cast(V)(v * 2.54); } static V fromCm(V)(V v) { return cast(V)(v / 2.54); } } enum inch = Inch.init; // Unit instance to use with abbreviated syntax.
Note: Two existing units a and c can't be retroactively extended with a direct conversion between them. This is by design, as it would break D's modularization/encapsulation approach (alternative: use mixin template for defining conversion functions, then it would be possible to have different behavior of the conversion function in each module). However, currently it is possible to create a third unit b which is convertible to both a and c, and then perform the conversion in two steps: convert!c(convert!b(1 * a))
Mixin template containing the implementation of the unit instance operators.
Furthermore, it marks the surrounding type as unit – every unit type has to mixin this template.
In addition, a unit type might also define a toString() function returning a custom unit symbol/name, and a list of Conversions. The example shows how a unit type for inches might be defined if scale and ScaledUnit did not exist (which remove the need to write boilerplate code like this).