preview in: it seems that return type auto solves scope variable assignment to this
Dmytro Katyukha
firemage.dima at gmail.com
Thu Feb 9 21:28:54 UTC 2023
On Thursday, 9 February 2023 at 17:59:39 UTC, Dennis wrote:
> On Thursday, 9 February 2023 at 17:42:00 UTC, Dmytro Katyukha
> wrote:
>> So, the first question is: where in this code assignment to
>> `this` happens?
>
> When calling a member function like `other.toAbsolute`, you're
> assigning `other` to the implicit `this` parameter, as if you
> are calling `toAbsolute(this: other)`.
>
>> So, the second question is: what happens in this case?
>
> Functions with `auto` return get attributes inferred, just like
> template functions. The compiler infers `scope` based on the
> implementation of `toAbsolute`.
Hi, thank you. Got it)
So, the next questions:
- Do it have sense to explicitly annotate all (most) member
methods of a struct with `scope` attribute? Or it is better to
use `auto` everywhere?.
- What drawbacks of annotating all (most) parameters as scope? Is
it correct approach?
- What about `in` parameter attribute? Why it is not used wide in
Phobos?
How to correctly call method from standard lib (for example
[expandTilde](https://dlang.org/phobos/std_path.html#expandTilde)) that does not annotated param with `scope` attribute?
For example:
```d
@safe unittest {
scope string p1 = "~/projects/d";
scope string p2 = expandTilde(p1);
assert(p1 != p2);
}
```
In this case, following error raised:
```
source/app.d(41,35): Error: scope variable `p1` assigned to
non-scope parameter `inputPath` calling `expandTilde`
```
Is it correct, to use `.dup` method?
```diff
@safe unittest {
scope string p1 = "~/projects/d";
- scope string p2 = expandTilde(p1);
+ scope string p2 = expandTilde(p1.dup);
assert(p1 != p2);
}
```
More information about the Digitalmars-d
mailing list