isConvertibleTo

Checks whether a quantity is convertible to a certain unit.

  1. bool isConvertibleTo(Q q)
    @safe
    bool
    isConvertibleTo
    (
    TargetUnit
    Q : Quantity!(U, V)
    U
    V
    )
    (
    Q q
    )
    if (
    isUnit!TargetUnit
    )
  2. bool isConvertibleTo(Q q)

Examples

assert(isConvertibleTo!gram(2 * kilogram));
assert(!isConvertibleTo!metre(2 * kilogram));
1 struct ConvertibleToFoo
2 {
3     mixin UnitImpl;
4     alias Conversions = AliasSeq!(Conversion!(Foo, toFoo, fromFoo));
5     static string toString(UnitString type = UnitString.name)
6     {
7         final switch (type)
8         {
9         case UnitString.name:
10             return "convertibleToFoo";
11         case UnitString.symbol:
12             return "ctf";
13         }
14     }
15 
16     static V toFoo(V)(V v)
17     {
18         return v * 3;
19     }
20 
21     static V fromFoo(V)(V v)
22     {
23         return v / 3;
24     }
25 }
26 
27 enum convertibleToFoo = ConvertibleToFoo.init;
28 
29 // »Regular« conversion.
30 auto c = 2 * convertibleToFoo;
31 assert(c.convert!foo == 6 * foo);
32 
33 // »Inverse« conversion.
34 auto f = 9 * foo;
35 assert(f.convert!convertibleToFoo == 3 * convertibleToFoo);
36 
37 static assert((2 * convertibleToFoo).isConvertibleTo!foo);
38 static assert(!(2 * convertibleToFoo).isConvertibleTo!bar);

Meta