Hi I am working through Programming in D. In one exercise I have a helper function in a struct
struct Fraction {
auto n = 0L;
auto d = 1L;
// ... other bits ...
ref Fraction reduce() {
import std.numeric : gcd;
v = gcd(n, d);
if (v > 1) {
n /= v;
d /= v;
}
return this;
}
// ... etc ...
}
void main() {
import std.stdio : writeln;
auto f = Fraction(3, 9);
writeln(f.reduce());
}
This works but issues a deprecation warning:
onlineapp.d(30): Deprecation: returning `this` escapes a reference to parameter `this`
onlineapp.d(30): perhaps annotate the parameter with `return`
Fraction(1, 3)
I found several other ways to make this work without the warnings, but I am wondering how to make this particular case work, or if it is not a recommended way, what the proper way would be.