relax inout rules?
Timon Gehr
timon.gehr at gmx.ch
Tue Dec 13 02:13:08 PST 2011
On 12/13/2011 07:26 AM, Walter Bright wrote:
> On 12/12/2011 10:56 AM, Steven Schveighoffer wrote:
>> If this ends up being viable, this is actually easier to explain than the
>> current rules for inout. We just have to make sure the rules are sound
>> before
>> doing something like this.
>
> I don't understand the point of doing this. Just make it const.
It does not work with const. (the fact that it currently does with DMD
is a severe bug).
Consider:
void bad(const(int)** x, const(int)* y){ *x=y; }
void main(){
immutable(int) i = 1;
int* x;
bad(&x, &i); // should error int** does not convert to const(int)**
// currently:
assert(x is &i); // mutable reference to immutable data
*x = 2; // BOOM!
}
On the other hand:
void main(){
immutable(int) i = 1;
immutable(int)* x;
bad(&x, &i); // fine, but the type checker cannot know that
}
Therefore it is sensible to allow
void good(inout(int)** x, inout(int)* y){ *x=y; }
void main(){
immutable(int) i = 1;
int* x;
good(&x, &i); // error, no value for inout makes this typecheck
}
On the other hand:
void good(inout(int)** x, inout(int)* y){ *x=y; }
void main(){
immutable(int) i = 1;
immutable(int)* x;
good(&x, &i); // this can now typecheck: use inout as immutable
}
More information about the Digitalmars-d
mailing list