Found by Coverty in LibreOffice

bearophile via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 10 04:06:12 PST 2014


Rikki Cattermole:

>> Cases like this remind me that division operator shouldn't 
>> accept a divisor of generic integer/floating/multi-precision
>> type, but a type that lacks a zero:
> ...
> Interesting.
> Code like this could be rather interesting.

In an old language like Ada you have to use explicitly a type 
that doesn't contain a zero (like a 1..5 range, or a natural, 
etc). But in a more modern language you can use "flow typing" (as 
seen in the Whiley language and elsewhere), so code similar to 
this (in D-like language) compiles:


int foo(in uint x, in uint y) {
     if (y == 0)
         throw new Exception("Bad");
     else {
         static assert(is(typeof(y) == Natural));
         return x / y;
     }
}

In Whiley inside the else branch the _type_ of y is different.

Bye,
bearophile


More information about the Digitalmars-d mailing list