1 /++
2 The purpose of this small library is to perform automatic compile-time or
3 run-time dimensional checking when dealing with quantities and units.
4 
5 There is no actual distinction between units and quantities, so there are no
6 distinct quantity and unit types. All operations are actually done on
7 quantities. For example, `meter` is both the unit _meter_ and the quantity _1m_.
8 New quantities can be derived from other ones using operators or dedicated
9 functions.
10 
11 Quantities can be parsed from strings at run time and compile time.
12 
13 The main SI units and prefixes are predefined. Units with other dimensions can
14 be defined by the user.
15 
16 Copyright: Copyright 2013-2018, Nicolas Sicard.
17 License: Boost License 1.0.
18 +/
19 module quantities;
20 
21 public import quantities.compiletime;
22 public import quantities.runtime;
23 public import quantities.common;
24 public import quantities.parsing;
25 public import quantities.si;
26 
27 /// Synopsis at compile-time
28 @("Synopsis at compile-time")
29 unittest
30 {
31     import quantities.compiletime;
32     import quantities.si;
33 
34     // Define a quantity from SI units
35     auto distance = 384_400 * kilo(meter);
36 
37     // Define a quantity from a string
38     auto speed = si!"299_792_458 m/s";
39     // Define a type for a quantity
40     alias Speed = typeof(speed);
41 
42     // Calculations on quantities
43     auto calculateTime(Length d, Speed s)
44     {
45         return d / s;
46     }
47     Time time = calculateTime(distance, speed);
48 
49     // Dimensions are checked at compile time for consistency
50     assert(!__traits(compiles, distance + speed));
51 
52     // Format a quantity with a format specification known at compile-time
53     assert(siFormat!"%.3f s"(time) == "1.282 s");
54 }
55 
56 /// Synopsis at run-time
57 @("Synopsis at run-time")
58 unittest
59 {
60     import quantities.runtime;
61     import quantities.si;
62     import std.exception : assertThrown;
63 
64     // Define a quantity from SI units (using the helper function `qVariant`)
65     auto distance = qVariant(384_400 * kilo(meter));
66 
67     // Define a quantity from a string
68     auto speed = parseSI("299_792_458 m/s");
69 
70     // Calculations on quantities (checked at compile time for consistency)
71     QVariant!double calculateTime(QVariant!double d, QVariant!double s)
72     {
73         return d / s;
74     }
75     auto time = calculateTime(distance, speed);
76 
77     // Dimensions are checked at run time for consistency
78     assertThrown!DimensionException(distance + speed);
79 
80     // Format a quantity with a format specification known at run-time
81     assert(siFormat("%.3f s", time) == "1.282 s");
82 }