Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 15, 2015 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
What is the difference between 'const' and 'in' parameter storage classes? When should I use 'const' or 'in'? The documentation says 'in' is the same as 'const scope' but I can't write 'const scope ref' though it's legal to write 'in ref'. Thank you |
May 15, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | 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. |
May 15, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On 5/15/15 12:04 PM, ref2401 wrote:
> What is the difference between 'const' and 'in' parameter storage classes?
> When should I use 'const' or 'in'?
>
> The documentation says 'in' is the same as 'const scope' but I can't
> write 'const scope ref' though it's legal to write 'in ref'.
scope ref const
-Steve
|
May 15, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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?
}
|
May 15, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 15 May 2015 at 16:30:29 UTC, Steven Schveighoffer wrote:
> On 5/15/15 12:04 PM, ref2401 wrote:
>> What is the difference between 'const' and 'in' parameter storage classes?
>> When should I use 'const' or 'in'?
>>
>> The documentation says 'in' is the same as 'const scope' but I can't
>> write 'const scope ref' though it's legal to write 'in ref'.
>
> scope ref const
>
> -Steve
still getting the error: Error: scope cannot be ref or out
|
May 15, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On Friday, 15 May 2015 at 18:18:13 UTC, ref2401 wrote:
> globalPtr = &ms; // is it legal?
No, but the compiler check isn't implemented.
|
May 18, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On 5/15/15 2:19 PM, ref2401 wrote:
> On Friday, 15 May 2015 at 16:30:29 UTC, Steven Schveighoffer wrote:
>> On 5/15/15 12:04 PM, ref2401 wrote:
>>> What is the difference between 'const' and 'in' parameter storage
>>> classes?
>>> When should I use 'const' or 'in'?
>>>
>>> The documentation says 'in' is the same as 'const scope' but I can't
>>> write 'const scope ref' though it's legal to write 'in ref'.
>>
>> scope ref const
>>
>
> still getting the error: Error: scope cannot be ref or out
interesting. Seems you would be right then.
The only other possibility could be ref scope const, but that doesn't seem right, I'll try it.
Nope, so basically there is no way to do in by expanding to scope const. This is something that should be considered if we ever want to modify what 'in' means.
I am not sure yet whether "in ref" should be valid or "scope ref" should be valid either. It doesn't seem to me that it should trigger an error.
-Steve
|
May 18, 2015 Re: 'const' and 'in' parameter storage classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Am Mon, 18 May 2015 09:05:51 -0400 schrieb Steven Schveighoffer <schveiguy@yahoo.com>: > On 5/15/15 2:19 PM, ref2401 wrote: > > On Friday, 15 May 2015 at 16:30:29 UTC, Steven Schveighoffer wrote: > >> On 5/15/15 12:04 PM, ref2401 wrote: > >>> What is the difference between 'const' and 'in' parameter storage > >>> classes? > >>> When should I use 'const' or 'in'? > >>> > >>> The documentation says 'in' is the same as 'const scope' but I can't write 'const scope ref' though it's legal to write 'in ref'. > >> > >> scope ref const > >> > > > > still getting the error: Error: scope cannot be ref or out > > interesting. Seems you would be right then. > > The only other possibility could be ref scope const, but that doesn't seem right, I'll try it. > > Nope, so basically there is no way to do in by expanding to scope const. This is something that should be considered if we ever want to modify what 'in' means. > > I am not sure yet whether "in ref" should be valid or "scope ref" should be valid either. It doesn't seem to me that it should trigger an error. > > -Steve Issue 8121 - "scope ref" is perfectly OK https://issues.dlang.org/show_bug.cgi?id=8121 -- Marco |
Copyright © 1999-2021 by the D Language Foundation