[Issue 23998] @mustuse should require opCast(T:bool) is checked
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Jun 18 19:19:23 UTC 2023
https://issues.dlang.org/show_bug.cgi?id=23998
Paul Backus <snarwin+bugzilla at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |snarwin+bugzilla at gmail.com
--- Comment #5 from Paul Backus <snarwin+bugzilla at gmail.com> ---
Dennis is correct. This is completely outside the purview of @mustuse. What you
are asking for is a guarantee not just that the object has been used, but that
it has been used *correctly* (according to some programmer-defined criteria).
For this specific example, I think the best solution is to redesign your Result
type so that it is impossible to access the wrapped value without performing a
null check. Here's one possible way to do so:
---
import core.attribute, std.stdio;
@mustuse struct Result {
private int* value;
bool isNull() => value is null;
int opApply(scope int delegate(int) dg)
{
if (value is null) return 0;
return dg(*value);
}
}
Result getResult() => Result(new int(42));
void main()
{
auto r = getResult();
foreach (int n; r)
writeln("got ", n);
}
---
--
More information about the Digitalmars-d-bugs
mailing list