Positive

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Oct 5 07:20:27 PDT 2008


KennyTM~ wrote:
> Bruce Adams wrote:
>> On Sun, 05 Oct 2008 11:18:24 +0100, KennyTM~ <kennytm at gmail.com> wrote:
>>>
>>> Probably they'd used (0, +∞) and [0, +∞) instead.
>>>
>>> ---
>>>
>>> BTW, negative real numbers can also be indicated as ℝ⁻ (R^- if 
>>> Unicode is not supported). Is it now special enough to deserve a 
>>> Negative!() template? :p
>>
>> Nice use of unicode.
>>
>>>
>>> I think these sign checking should be done through contracts or 
>>> "conditional template" (? whatever it's called; I haven't used one of 
>>> these yet) instead. Unless you can runtime check that
>>>
>>>    Positive!(double) x = 6;
>>>    Positive!(double) y = 12;
>>
>> Compile time check
>>
>>>    Positive!(double) z = void;
>>
>> This must be an syntax error surely?
> 
> type x = void; means don't initialize x. Or have I missed something?
> 
>>
>>>    z = x - y;  // raises error.
>>
>> The return type of subtraction goes outside the postive domain:
>> i.e. if it needed to exist (which it probably doesn't) the function 
>> spec would be:
>>
>> double opMinus(Positive!(double) y, Positive!(double) y);
>>
>>
>> So this is actually:
>>
>> z = cast(Positive!(double))(x - y)
>>
>> Hence there is the necessary check which could be either run-time or 
>> compile time.
>>
> 
> Most of the time it can't be compile time because if I've initialized x 
> and y to be some runtime-dependent function (e.g. sin(current time)) 
> then you can't be sure of the sign of x-y. So the check must be made 
> into runtime unless the expression can be CTFE-ed.
> 
>>
>>>    Positive!(double) w;
>>>    din.readf("%g", &w);  // raises error if user enters negative number.
>>>
>>> But I don't think uint, etc now even do these checks.
>>
>> This would ideally work as above but I see your problem.
>> The contract of readf is not tight enough. You should rewrite as:
>>
>> double wtemp;
>> din.read("%g", &wtemp);
>> Positive!(double) w = wtemp;    // type check applied here
>>
> 
> But this is getting ugly :)

Of course. It's because it uses the "!". :o)

Andrei



More information about the Digitalmars-d mailing list