Proposal to add 'Elements of Programming' Concepts to std.traits
Guillaume Chatelet
chatelet.guillaume at gmail.com
Sun Jun 17 01:20:50 PDT 2012
That's a *lot* more code but I think we could do something like
struct NoZero {
private int _value;
invariant() { assert(_value != 0); }
this(int x) { _value = x; }
NoZero opBinary(string op)(const ref NoZero rhs) {
return NoZero(mixin("this._value"~op~"rhs._value"));
}
NoZero opBinary(string op)(int rhs) {
return NoZero(mixin("this._value"~op~"rhs"));
}
ref NoZero opUnary(string op)() if(op=="++"||op=="--"){
mixin(op~"this._value;");
return this;
}
ref NoZero opAssign(int rhs) {
value = rhs;
return this;
}
@property int value() const { return _value; }
@property void value(int newValue) { _value = newValue; }
alias this value;
}
void main(){
NoZero a = 1;
auto b = a; // copyable
// --b; would assert
auto c = b+1;
auto d = c+a;
}
Note the implementation is incomplete, still need to implement
comparison to bool, operator'!' , unary '+', '-', '~', checks for
overflows. It's a bit of work but I think it can be done.
More information about the Digitalmars-d
mailing list