Why D const is annoying

Mehrdad wfunction at hotmail.com
Sat Dec 10 11:03:12 PST 2011


On 12/10/2011 3:33 AM, Walter Bright wrote:
> On 12/10/2011 3:14 AM, Mehrdad wrote:
>> ... and another... (yes, this one _IS_ a const issue)
>>
>> struct S { int opApply(scope int delegate(ref inout(int)) dg) inout { 
>> return 0; } }
>> void main()
>> {
>> foreach (i; S()) { }
>> }
>>
>> Error: inout on parameter means inout must be on return type as well 
>> (if from D1
>> code, replace with 'ref')
>
> Right. inout has no point if it also does not appear on the return type.
>
> The purpose of inout is to transfer the 'constness' of the argument to 
> the return type. If inout isn't on the return type somewhere, there's 
> likely a design mistake in your code. It's like having:
>
> {
>     a + b;
> }
>
> Which is an error in D because such is pointless.
Sure, it's always a design mistake. :-)

So how are you supposed to implement opApply on a container (or e.g. 
here, a matrix)? Copy/paste the code for const- and non-const versions?


More information about the Digitalmars-d mailing list