'strong types' a la boost
Namespace via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Mar 14 09:01:14 PDT 2015
You can do it this way:
----
struct dollars_t {
uint _dollar;
this(uint d) {
_dollar = d;
}
alias _dollar this;
}
struct cents_t {
uint _cent;
this(uint c) {
_cent = c;
}
alias _cent this;
}
void do_something_with_dollars(dollars_t d) {
writeln(d);
}
void main() {
dollars_t d = 1;
do_something_with_dollars(d);
cents_t c = 2;
//do_something_with_dollars(c);
//do_something_with_dollars(2);
}
----
Or you can create your own small TypeDef:
----
struct TypeDef(T, size_t l = __LINE__) {
T _val;
this(T v) {
_val = v;
}
alias _val this;
}
alias dollars_t = TypeDef!(uint);
alias cents_t = TypeDef!(uint);
----
Thanks to the second template parameter 'l' the template
instances of dollars_t and cents_t aren't equal.
More information about the Digitalmars-d-learn
mailing list