Why D const is annoying

Mafi mafi at example.org
Sun Dec 11 13:56:20 PST 2011


Am 11.12.2011 20:35, schrieb Andrei Alexandrescu:
> On 12/11/11 12:40 PM, Mafi wrote:
>> void f(ref const(int)* a, const(int)* b) {a = b; }
>> void main() {
>> immutable(int)* a;
>> auto b = (new int[](5)).ptr;
>> f(a, b);
>> //if this compiles I have just assigned a mutable pointer
>> //to an immutable one
>> }
>
> immutable(int)* does not convert to immutable(const)*.
>
> Andrei
[assuming you meant ref const(int)*]

But look:
I want a function f(a,b) so that it copies int* b into a. I want this 
function to be const correct and not to be a template.
If I'm not mistaken, in the planned D (ie without bugs and with 
everything implememnted correctly) there's no way to express such a 
function.
I see two solutions:
1. Allow  calling void f(ref const(int)* a, const(int)* b) {a = b; } 
with immutable parameter a.
2. Allow void f(ref inout(int)* a, inout(int)* b) { a = b; } using my 
rule I suggested before.

I think Walter suggests number 1 but as I showed it violates the const 
system.
I think allowing inout with ref/out parameters is right even without the 
return value being inout. These ref/out parameters are conceptionally 
results.
I should also be allowed if delegate/function has a inout parameter. So 
void g(inout int a, delegate(inout(int)*)) should be allowed.
This delegate is a callback and it's parameter is conceptionally the 
result of this function.

In my opinion we definitly have to change the current strict inout-rule.

Mafi


More information about the Digitalmars-d mailing list