www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Ada-Style Range Types with Value Range Propagation in Arithmetic

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I'm trying to extend my wrapper class Bound to support CTFE Value 
Range Propagation in primary in opUnary and opBinary similar to 
what Ada's Range Types.

My current try so far for binary arithmetic is

     auto opBinary(string op, U,
                   U lower_rhs = U.min,
                   U upper_rhs = U.max,
                   string file = __FILE__,
                   int line = __LINE__)(Bound!(U, lower_rhs, 
upper_rhs) rhs)
     {
         Bound!(CommonType!(T, U),
                min(T.min, U.min),
                max(T.max + U.max)) tmp = void; // TODO: Needs fix
         mixin("tmp = _value " ~ op ~ "rhs;");
         mixin(check());
         return tmp;
     }

but this errors as

bound.d(120,19): Error: undefined identifier U and I don't 
understand why I can't parameterize opBinary in this way.

Ideas anyone?

For reference, this older version

https://github.com/nordlow/justd/blob/master/bound.d

compiles but do not do value range propagation in arithmetic.
Mar 19 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 Ideas anyone?
I cracked it: auto opBinary(string op, U, string file = __FILE__, int line = __LINE__)(U rhs) { static if (is(U == Bound)) { alias C = CommonType!(T, U.type); Bound!(C, cast(C)min + cast(C)U.min, cast(C)max + cast(C)U.max) tmp = void; // TODO: Needs fix } else { CommonType!(T, U) tmp = void; } mixin("tmp = _value " ~ op ~ "rhs;"); mixin(check()); return tmp; }
Mar 19 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 I cracked it:
This is important stuff. So if you find that you can't do something in D, consider asking for it in the main D newsgroup and in Bugzilla. Bye, bearophile
Mar 22 2014