Writing Bug-Free C/D Code

Derek Parnell derek at psych.ward
Tue Mar 20 03:16:12 PDT 2007


On Tue, 20 Mar 2007 00:18:24 -0800, David B. Held wrote:

> Derek Parnell wrote:
>> [...]
>>  void main()
>>  {
>>    age x;
>>    len y;
>>    float z = 4.321;
>> 
>>    x = 10;
>>    y = 5;
>>    //y = z; // fails but probably shouldn't
> 
> I disagree.  Consider:
> 
> int x = 15;
> months m = x; // Sound?

Of course it is. Unless you are talking about months-per-year, but that's
not 'months' is it?

The trouble is that D's typedef does not allow you to extend the semantics
of the base type, but that's what structs and classes are for, no?

The Euphoria language has a slightly different type construction mechanism
which has its good side and bad side, but maybe something like it could be
used to extend D's typedef mechanism.

 type month(object x)
    if not integer(x) then
       return 0
    end if

    if x < 1 then
       return 0
    end if

    if x > 12 then
       return 0
    end if

    return 1
 end type


 month m
 m = 15 -- FAILS with a type mismatch error.

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell



More information about the Digitalmars-d mailing list