Read Once then reset/init value?
Simen Kjærås
simen.kjaras at gmail.com
Tue Oct 29 23:28:35 UTC 2019
On Tuesday, 29 October 2019 at 22:24:20 UTC, Robert M. Münch
wrote:
> I quite often have the pattern where a value should be read
> just once and after this reset itself. The idea is to avoid
> that others read the value by accident and get an older state,
> instead they get an "invalid/reset" value.
>
> Is there a library function that can mimic such a behaviour?
Something like this?
T readOnce(T)(ref T value) {
auto tmp = value;
value = T.init;
return tmp;
} unittest {
int i = 3;
assert(i.readOnce == 3);
assert(i == 0);
}
If so, no, there is no library function for it, but feel free to
use the above. You may very well have to change T.init to
something more fitting for your use case, of course.
If this is not what you need, feel free to explain further, as
I'm not sure I understood you correctly. :)
--
Simen
More information about the Digitalmars-d-learn
mailing list