Writing const-correct code in D

Andrew Fedoniouk news at terrainformatica.com
Wed Mar 8 14:46:18 PST 2006


>>
>> Let's say we have two declarations:
>>
>> T[] and readonly T[]
>>
>> These two describe two different types - they have two different sets of 
>> methods:
>>
>> T[] has opSliceAssign and opIndexAssign.
>> in contrary readonly T[] has no such methods.
>>
>> This is why they are two distinct types. And it is highly desirable that 
>> they will be treated
>> as types and not as any sort of contracts. Think about template 
>> instantiation, static if's and so on.
>
> Is it necessary that there are two distinct types? I feel it would be 
> enough, if there was one type with two sets of methods, one of which could 
> always be called, and the other only through a non-readonly reference.
>
> Are there any benefits to having two distinct types?

Logically it is one type - region  from ptr and up to ptr+length
But in this two cases it has different set of operations.
You may *interpret* it as different types.

readonly is a filter - it filters out all mutators from base type.

static if( T[] has(method) opSliceAssign) -> compile-time-true and
static if( readonly T[] has(method) opSliceAssign ) -> compile-time-false

Andrew Fedoniouk.
http://terrainformatica.com










More information about the Digitalmars-d mailing list