Read Once then reset/init value?
Robert M. Münch
robert.muench at saphirion.com
Mon Nov 4 07:32:54 UTC 2019
On 2019-10-29 23:28:35 +0000, Simen Kjærås said:
> 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.
Hi, that looks very good. I forgot about the UFCS possibility. That's
very good because it works on basic types too. Thanks :-)
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
More information about the Digitalmars-d-learn
mailing list