Asdf.opCast

cast operator overloading.

struct Asdf
T
opCast
(
T
)
()

Examples

null

import std.math;
import asdf.serialization;
auto null_ = serializeToAsdf(null);
interface I {}
class C {}
assert(cast(uint[]) null_ is null);
assert(cast(uint[uint]) null_ is null);
assert(cast(I) null_ is null);
assert(cast(C) null_ is null);
assert(isNaN(cast(double) null_));
assert(! cast(bool) null_);

boolean

import std.math;
import asdf.serialization;
auto true_ = serializeToAsdf(true);
auto false_ = serializeToAsdf(false);
static struct C {
    this(bool){}
}
auto a = cast(C) true_;
auto b = cast(C) false_;
assert(cast(bool) true_ == true);
assert(cast(bool) false_ == false);
assert(cast(uint) true_ == 1);
assert(cast(uint) false_ == 0);
assert(cast(double) true_ == 1);
assert(cast(double) false_ == 0);

numbers

import std.bigint;
import asdf.serialization;
auto number = serializeToAsdf(1234);
auto zero = serializeToAsdf(0);
static struct C
{
    this(in char[] numberString)
    {
        assert(numberString == "1234");
    }
}
auto a = cast(C) number;
assert(cast(bool) number == true);
assert(cast(bool) zero == false);
assert(cast(uint) number == 1234);
assert(cast(double) number == 1234);
assert(cast(BigInt) number == 1234);
assert(cast(uint) zero == 0);
assert(cast(double) zero == 0);
assert(cast(BigInt) zero == 0);

string

import std.bigint;
import asdf.serialization;
auto number = serializeToAsdf("1234");
auto false_ = serializeToAsdf("false");
auto bar = serializeToAsdf("bar");
auto zero = serializeToAsdf("0");
static struct C
{
    this(in char[] str)
    {
        assert(str == "1234");
    }
}
auto a = cast(C) number;
assert(cast(string) number == "1234");
assert(cast(bool) number == true);
assert(cast(bool) bar == true);
assert(cast(bool) zero == false);
assert(cast(bool) false_ == false);
assert(cast(uint) number == 1234);
assert(cast(double) number == 1234);
assert(cast(BigInt) number == 1234);
assert(cast(uint) zero == 0);
assert(cast(double) zero == 0);
assert(cast(BigInt) zero == 0);

For ASDF arrays and objects cast(T) just returns this.deserialize!T.

import std.bigint;
import asdf.serialization;
assert(cast(int[]) serializeToAsdf([100, 20]) == [100, 20]);

UNIX Time

import std.datetime;
import asdf.serialization;

auto num = serializeToAsdf(0.123456789); // rounding up to usecs
assert(cast(DateTime) num == DateTime(1970, 1, 1));
assert(cast(SysTime) num == SysTime(DateTime(1970, 1, 1), usecs(123456), UTC())); // UTC time zone is used.

Meta