xxxxxxxxxx
import std.stdio;
enum DisplayUnit : int { init = 0 }
enum Width : DisplayUnit { init = DisplayUnit.init }
enum Height : DisplayUnit { init = DisplayUnit.init }
void foo()
{
Width width = cast(Width)16;
Height height = cast(Height)9;
//width = height; // illegal implicit conversion
//width = 4; // illegal implicit conversion
width = cast(Width)height; // legal, force conversion
DisplayUnit val = width; // allowed implicit conversion, Width "inherits" DisplayUnit
int num = width; // also allowed, Width "inherits" DisplayUnit which "inherits" int
writeln(num); // 9
writeln(width); // cast(Width)9
}
// you can also make helper functions for prettier syntax:
DisplayUnit px(int val) { return cast(DisplayUnit) val; }
Width width(DisplayUnit unit) { return cast(Width) unit; }
Height height(DisplayUnit unit) { return cast(Height) unit; }
void bar()
{
const width = .width(833.px);
const height = .height(480.px);
auto ratioI = width / height; // we can calculate as if this is a normal int
auto ratioF = width / cast(float)height; // and of course cast to any type
writeln(ratioI); // 1
writeln(ratioF); // 1.73542
}
void main()
{
foo();
bar();
}