null and type safety

KennyTM~ kennytm at gmail.com
Fri Nov 7 03:32:46 PST 2008


BCS wrote:
> Reply to Walter,
> 
>> Steven Schveighoffer wrote:
>>
>>> Couldn't one design a struct wrapper that implements this behavior?
>>>
>> If that cannot be done in D, then D needs some design improvements.
>> Essentially, any type should be "wrappable" in a struct which can
>> alter the behavior of the wrapped type.
>>
>> For example, you should also be able to create a ranged int that can
>> only contain values from n to m:
>>
>> RangedInt!(N, M) i;
>>
>> Preserving this property of structs has driven many design choices in
>> D, particularly with regards to how const fits into the type system.
>>
> 
> Why not explicitly support this with bodied typedefs?
> 
> 
> typedef int MyInt(int m, int M)
> {
>    MyInt opAssign(int i) { assert(m <= i && i <= M); this = i; }
> 
>    MyInt opAssign(int m_, int M_)(MyInt!(m_,M_) i) // <- that doesn't 
> work but...
>    {
>       static if(m > m_ && M_ > M) assert(m <= i && i <= M);  // only 
> assert as needed
>       else static if(m > m_) assert(m <= i);
>       else static if(M < M_) assert(i <= M);
>       this = i;
>    }
> 
>    // this is only to define the return type, the normal code for int is 
> still generated
>    MyInt!(m+m_, M+M_) opAdd(int m_, int M_)(MyInt!(m_,M_) i) = this; }
> 
> 

Just create a struct with an int as its only member?



More information about the Digitalmars-d mailing list