Why D const is annoying

Timon Gehr timon.gehr at gmx.ch
Sat Dec 10 13:00:58 PST 2011


On 12/10/2011 08:47 PM, Mehrdad wrote:
> 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).

Yah, the issue is that *BOTH* your inouts are on a parameter. Just 
change them to const, without copying any code and call it a day...


More information about the Digitalmars-d mailing list