On Tuesday, 15 April 2025 at 21:06:30 UTC, WhatMeWorry wrote:
>I had assumed that ref keyword negated the use of * and & operators but when I try to use it, DMD is saying "only parameters, functions and foreach
declarations can be ref
". Fair enough. I thought that the * and & operator was for when interfacing with C/C++ code. Since this is entirely in D code, I thought there might be other options.
These are accurate observations! As you know, classes can be more practical in the programming world. Have you also tried looking at the topic from the following perspective?
class S
{
char c;
float f;
override string toString()
=> text(i"[c = $(c), f = $(f)]");
}
import std.conv, std.stdio;
void main()
{
S[int] aa; // int -> S as an associative array (not by address but by direct reference)
auto u = new S();
u.c = 'a';
u.f = 1.33;
aa[0] = u;
auto v = new S();
v.c = 'z';
v.f = 3.14;
aa[1] = v;
aa.writeln;
S d = aa[0];
d.writeln;
d = null; // d no longer points to the object, but aa[0] still does
// let's change the u object:
u.c ^= ' '; // magic!
u.f *= 3;
assert(d is null);
d = aa[0];
d.writeln;
assert(d is null);
aa.writeln;
} /* Prints:
[0:[c = a, f = 1.33], 1:[c = z, f = 3.14]]
[c = a, f = 1.33]
[c = A, f = 3.99]
[0:[c = A, f = 3.99], 1:[c = z, f = 3.14]]
//*/
SDB@79