'scope' confusion
Andy
andy.pj.hanson at gmail.com
Fri Apr 8 05:52:09 UTC 2022
In the following code, I can pass a non-`scope` argument to a
function expecting a `scope` parameter, but I can't pass a
`scope` argument. That seems backwards?
```
@safe @nogc pure nothrow:
void main() {
string[2] notScope = ["one", "two"];
f(notScope);
scope string[2] yesScope = ["one", "two"];
f(yesScope);
}
void f(scope string[] expectingScope) {}
```
Output (of `dmd a.d -preview=dip1000`):
```
a.d(8): Error: cannot take address of `scope` local `yesScope` in
`@safe` function `main`
a.d(8): Error: cannot cast expression `yesScope` of type
`string[2]` to `string[]`
```
The error only happens in dmd 2.099.0 -- it didn't happen in
2.098.1 .
------
There is another even simpler case that doesn't work in either
version:
```
@safe @nogc pure nothrow:
void main() {
scope string[2] xs = ["one", "two"];
foreach (scope immutable string x; xs) {}
}
```
Output (of `dmd a.d -preview=dip1000`):
```
a.d(5): Error: cannot take address of `scope` local `xs` in
`@safe` function `main`
```
More information about the Digitalmars-d
mailing list