Thank you!

Bastiaan Veelo Bastiaan at Veelo.net
Tue Sep 7 21:43:02 UTC 2021


On Tuesday, 7 September 2021 at 13:36:39 UTC, Tejas wrote:
>
> ```d
> import std.stdio;
>
> struct S { int i; }
>
> S _s;
>
> S s() {return _s;}
>
> void main()
> {
>     s().i = 42;
>     writeln(_s.i == 42);
> }
>
> Output:
> false
> ```
>
> Wow, compiler help much appreciated

Is this confusing to you? I might be missing your point. It isn’t 
confusing to me, `struct`s are value types. If you want `s()` to 
return a reference to `_s` it should be declared as returning a 
reference:

```d
import std.stdio;

struct S { int i; }

S _s;

ref S s() {return _s;}

void main()
{
     s().i = 42;
     writeln(_s.i == 42);
}

Output:
true
```

—Bastiaan.


More information about the Digitalmars-d mailing list