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));
struct ConvertibleToFoo
{
    mixin UnitImpl;
    alias Conversions = AliasSeq!(Conversion!(Foo, toFoo, fromFoo));
    static string toString(UnitString type = UnitString.name)
    {
        final switch (type)
        {
        case UnitString.name:
            return "convertibleToFoo";
        case UnitString.symbol:
            return "ctf";
        }
    }

    static V toFoo(V)(V v)
    {
        return v * 3;
    }

    static V fromFoo(V)(V v)
    {
        return v / 3;
    }
}

enum convertibleToFoo = ConvertibleToFoo.init;

// »Regular« conversion.
auto c = 2 * convertibleToFoo;
assert(c.convert!foo == 6 * foo);

// »Inverse« conversion.
auto f = 9 * foo;
assert(f.convert!convertibleToFoo == 3 * convertibleToFoo);

static assert((2 * convertibleToFoo).isConvertibleTo!foo);
static assert(!(2 * convertibleToFoo).isConvertibleTo!bar);

Meta