Read Once then reset/init value?

Jacob Carlborg doob at me.com
Wed Oct 30 11:53:42 UTC 2019


On 2019-10-30 00:28, Simen Kjærås wrote:

> 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);
> }

Perhaps better to encapsulate it in a struct to avoid someone accessing 
the value directly.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list