[your code here]

bearophile bearophileHUGS at lycos.com
Sat Feb 11 07:47:01 PST 2012


H. S. Teoh:

> > bool isKaprekar(in long n) pure nothrow
> > in {
> >     assert(n > 0, "isKaprekar(n): n must be > 0");
> >     assert(n <= uint.max, "isKaprekar(n): n must be <= uint.max");
> > } body {
> [...]
> 
> Shouldn't you just use "in ulong n" as parameter instead of long with a
> contract?

In this case the answer is probably positive.

But in general it's better to accept a signed number and then refuse the negative values in the pre-condition, otherwise if you give by mistake a negative number to the function it's not caught.

Such work-arounds are less needed in saner languages, where the ranges of integral values are verified, at compile time where possible, and at run-time otherwise. Unwanted wrap-arounds and undetected overflows in integral values are so '70 :-)

Bye,
bearophile


More information about the Digitalmars-d mailing list