RFC: Value range propagation for if-else
Lionello Lunesu via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jun 18 01:12:17 PDT 2014
On 18/06/14 15:53, bearophile wrote:
>> 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.
Most other traits return tuples, so it seemed like a good fit.
> I presume your proposal allows this code to compile:
>
> int x = 100;
> void main() {
> if (x >= 0 && x <= ubyte.max) {
> ubyte y = x;
> }
> }
Yes, provided 'x' is immutable or const. It can be extended for mutable
values as well, but then mutations/gotos must be tracked.
> 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;
> }
Yeah, I wanted to support "assert" as well, but it doesn't create a
scope so it'll be a bit trickier to implement.
L.
More information about the Digitalmars-d
mailing list