Why D const is annoying

Mehrdad wfunction at hotmail.com
Sat Dec 10 11:47:15 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.

Oh yeah, I just realized -- I think you missed the _second_ 'inout'.
It's indeed on a parameter! It's just on the implicit 'this' parameter, 
not on an explicit one. This is a bug (IMO, arising from the fact that 
inout() is treated like a type constructor, whereas it shouldn't be).


More information about the Digitalmars-d mailing list