digitalmars.D.learn - Read Once then reset/init value?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (9/9) Oct 29 2019 I quite often have the pattern where a value should be read just once
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (19/24) Oct 29 2019 Something like this?
- Jacob Carlborg (5/16) Oct 30 2019 Perhaps better to encapsulate it in a struct to avoid someone accessing
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (31/45) Oct 30 2019 Quite possibly, but the post was somewhat low on details, and
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (7/30) Nov 03 2019 Hi, that looks very good. I forgot about the UFCS possibility. That's
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? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Oct 29 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
Oct 29 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
Oct 30 2019
On Wednesday, 30 October 2019 at 11:53:42 UTC, Jacob Carlborg wrote:On 2019-10-30 00:28, Simen Kjærås wrote:Quite possibly, but the post was somewhat low on details, and encapsulating it like that does put certain limits on how it can be used, so it's not necessarily the best idea. FWIW, here's one possible way to do it with a struct: struct Readonce(T, T defaultValue = T.init) { private T value; alias get this; T get() { auto tmp = value; value = defaultValue; return tmp; } void get(T newValue) { value = newValue; } this(T newValue) { value = newValue; } } unittest { Readonce!(int, -1) a = 3; assert(a == 3); assert(a == -1); a = 3; assert(a == 3); assert(a == -1); } -- SimenSomething 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.
Oct 30 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: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 | fasterI 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.
Nov 03 2019