Hacker News new | ask | show | jobs
by jeremiep 3884 days 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.

1 comments

std.typecons.Typedef does something similar: http://dlang.org/phobos/std_typecons.html#.Typedef