Writing const-correct code in D

xs0 xs0 at xs0.com
Wed Mar 8 12:29:33 PST 2006


Andrew Fedoniouk wrote:
>>I think having seperate overloads for const and non-const parameters is a 
>>design mistake in C++. I see C++ 'const' as a compile-time contract, not a 
>>type. IMHO, overloading const vs non-const is like overloading functions 
>>based on their contracts.
>>
>>I suspect that 'const' has exaggerated importance to those of us from a 
>>C++ background, because it's almost the only contract that C++ provides.
>>Maybe the solution to 'const' is a more general compile-time contract 
>>mechanism.
> 
> 
> 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?


xs0



More information about the Digitalmars-d mailing list