Should this work or a bug in compiler?

An home at home.com
Sat Nov 7 20:10:27 UTC 2026


On Saturday, 7 November 2026 at 03:55:46 UTC, monkyyy wrote:
> On Saturday, 7 November 2026 at 03:46:47 UTC, apz28 wrote:
>>     struct S
>>     {
>>         int x;
>>
>>         ref S byRef() return
>>         {
>>             return this;
>>         }
>>     }
>>
>>     void main()
>>     {
>>         import std.conv : to;
>>         import std.stdio : writeln;
>>
>>         S s;
>>
>>         s.x = 1;
>>         auto s2 = s.byRef();
>>         s2.x = 2;
>>         writeln(&s, " vs ", &s2);
>>         assert(s2.x == 2, s2.x.to!string());
>>         assert(s.x == 2, s.x.to!string());
>>     }
>>
>>
>> Output from v2.113
>> 7FFF27E25958 vs 7FFF27E2595C
>> core.exception.AssertError at onlineapp.d(23): 1
>> ----------------
>> ??:? _d_assert_msg [0x5ef4a93a89d0]
>> ./onlineapp.d:23 _Dmain [0x5ef4a93841c7]
>
> thats not what ref means, i think what you want is in 
> std.typecons; dont tho, if you want a pointer take a pointer.

The whole point of `ref` is safe to avoid `null` pointer value




More information about the Digitalmars-d-learn mailing list