Open jdonnerstag opened 3 years ago
The C-snippet confirms my understanding
rosie__parser__rpl__Intar data = ((new_array_from_c_array(7, 7, sizeof(int), _MOV((int[7]){1, 2, 5, 9, 3, 6, 0}))));
rosie__parser__rpl__MyIter _t1 = rosie__parser__rpl__Intar_my_filter(data);
while (1) {
Option_int _t2 = rosie__parser__rpl__MyIter_next(&_t1);
if (_t2.state != 0) break;
int x = *(int*)_t2.data;
eprintln( str_intp(2, _MOV((StrIntpData[]){{_SLIT("x: "), 0xfe07, {.d_i32 = x}}, {_SLIT0, 0, { .d_c = 0 }}})));
}
rosie__parser__rpl__MyIter iter = rosie__parser__rpl__Intar_my_filter(data);
rosie__parser__rpl__MyIter _t3 = iter;
while (1) {
Option_int _t4 = rosie__parser__rpl__MyIter_next(&_t3);
_t1 and _t3 are (internal) clones not access from V. At least in the 2nd example, _t3 is superfluous and not needed. V needs to detect that a variables has been declared already and leverage that, rather then making another copy.
A workaround is not complicated, but it took me half a day to realise what is actually going wrong. Of course first I assumed I'm doing something wrong, until I figured it out. If properly supportting it is too complicated or cumbersome at first, then at least a useful warning or error message would be good.
mut iter := data.my_filter()
for {
x := iter.next() or { break }
V version: V 0.2.4 52df19e OS: windows, Microsoft Windows 10 Enterprise v19042 64-bit
What did you do?
What did you expect to see? I expected to see that at (1) the updated
pos
value gets printed. Instead it is always the initial value. Which means that even in the 2nd test case (for x in iter {
), V clones the object. And that means, I don't have access to it. And I think there is no way, to get access to the iter object that is being used by next().What did you see instead? At (1) always the initial value of
pos
is printed, but never the updated / real one.