RFC: Value range propagation for if-else

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 00:53:26 PDT 2014


Lionello Lunesu:

> I've also added a __traits(intrange, <expression>) which 
> returns a tuple with the min and max for the given expression.

I'd like a name like integral_range, and perhaps it's better for 
it to return a T[2] (fixed size array) instead of a tuple.

Such __traits(integral_range, exp) could work on core.checkedint 
values too, or it could be used by them to remove some overflow 
tests and increase their performance.


> It's used in the test case as follows:

I presume your proposal allows this code to compile:

int x = 100;
void main() {
     if (x >= 0 && x <= ubyte.max) {
         ubyte y = x;
     }
}


If your proposal is extended to contracts, you can also write:


ubyte foo(immutable int x)
in {
     assert(x >= 0 && x <= ubyte.max);
} body {
     return x;
}
void main() {}


Bye,
bearophile


More information about the Digitalmars-d mailing list