Fghj.opCast

cast operator overloading.

struct Fghj
T
opCast
(
T
)
()

Examples

null

import std.math;
import fghj.serialization;
auto null_ = serializeToFghj(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 fghj.serialization;
auto true_ = serializeToFghj(true);
auto false_ = serializeToFghj(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 fghj.serialization;
auto number = serializeToFghj(1234);
auto zero = serializeToFghj(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 fghj.serialization;
auto number = serializeToFghj("1234");
auto false_ = serializeToFghj("false");
auto bar = serializeToFghj("bar");
auto zero = serializeToFghj("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 FGHJ arrays and objects cast(T) just returns this.deserialize!T.

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

UNIX Time

import std.datetime;
import fghj.serialization;

auto num = serializeToFghj(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