3 ms·
I rather like the implicit conversions. Especially in D where they only happen where its safe. There's the 'to' function to perform checked conversions and the
by jeremiep 11y ago
I rather like the implicit conversions. Especially in D where they only happen where its safe.
There's the 'to' function to perform checked conversions and the standard cast operator to perform unchecked conversions.
int foo = 256;
ubyte bar = foo.to!ubyte; // throws ConvException
ubyte baz = cast(ubyte) foo; // overflows as expected
If I want more type-safety than this, I'll create simple wrappers:
struct Seconds { int value; alias value this; }
That way I can still pass seconds everywhere an int is expected, but I can now declare arguments to only accept Seconds and not ints.
- p0nce 11y agostd.typecons.Typedef does something similar: http://dlang.org/phobos/std_typecons.html#.Typedef http://dlang.org/phobos/std_typecons.html#.Typedef