Small question.
I have one var m which is computed when needed.
And computing m must be never or once.
Can we write this smarter / shorter in some nice oneliner?
var m: ?i32 = null;
// ...
// earlier code maybe computiing m, maybe not
// ...
if (m == null) {
m = compute_m(...);
}
if (m.? <= 300) {
// code
}
there used to be std.once which did this. it was removed with the move to std.Io iirc, but you could model your thing on it, with the knowledge that it was removed for good reason
Ok. thanks. Yes something like that entered my mind. But it still feels hm.. how do i say that. clunky?
Whatever solution there is, it involves extra variables / functions.
I think I will let it be the way it is. here
edit: altough looking again… i think i can indeed change it logically to calculate once when needed.
I believe the following qualifies as a nice oneliner:
var m: ?i32 = null;
// some work...
// if we want to update m for later use
m = m orelse compute_m();
if (m.? <= 300) {
// ...
}
// if we don't care for updating it
if (m orelse compute_m() <= 300) {
// ...
}