Positive

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Oct 5 06:47:30 PDT 2008


Michel Fortin wrote:
> On 2008-10-05 01:55:43 -0400, Andrei Alexandrescu 
> <SeeWebsiteForEmail at erdani.org> said:
> 
>> That will do a runtime check and throw if x is negative. That's 
>> unprecedented for an implicit cast, so I was thinking of defining a
>> universal "unsigned" template function that does the check:
>>
>> auto y = sqrt(unsigned(x));
> 
> Well, at this point I would prefer if it was a contract. Having to 
> explicitly convert to unsigned just makes it harder than necessary.

But the point is that often you already traffic in nonnegatives, so you 
have them handy. Only passing the first time is difficult. But I hear 
you. We can make the conversion implicit.

> Throwing may be unprecedented for implict casts, but it is standard for 
> contracts using assertions. So I think it would be okay to implicit cast 
> and assert the value is non-negative (which would be equivalent to 
> enforcing a contract).
> 
> But then, shouldn't sqrt(-1) give you NaN, or i?

It can't yield i because i has a different type. It gives NaN now, and 
believe me, that doesn't help any.


Andrei



More information about the Digitalmars-d mailing list