'const' and 'in' parameter storage classes
ref2401 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri May 15 11:18:11 PDT 2015
On Friday, 15 May 2015 at 16:08:31 UTC, Adam D. Ruppe wrote:
> The scope storage class means you promise not to escape any
> reference to the data. This isn't enforced but it is similar in
> concept to Rust's borrowed pointers - it may someday be
> implemented to be an error to store them in an outside variable.
>
> Only use 'in' if you are looking at the data, but not modifying
> or storing copies of pointers/references to it anywhere in any
> way.
I expected the compiler forbids 'in' params escaping.
struct MyStruct {
this(int a) {
this.a = a;
}
int a;
}
const(MyStruct)* globalPtr;
void main(string[] args) {
MyStruct ms = MyStruct(10);
foo(ms);
ms.a = 12;
writeln("global: ", *globalPtr); // prints const(MyStruct)(12)
}
void foo(in ref MyStruct ms) {
globalPtr = &ms; // is it legal?
}
More information about the Digitalmars-d-learn
mailing list