You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varmatrix= [0x1000][0x1000]u32;
for (matrix) |row| {
for (row) |cell| {
print(cell);
}
}
In this example, row with type [0x1000]u32 is possibly a copy of the row from the matrix. This would be improved by capturing |*row| instead, but now row can mutate the matrix, which isn't what I want.
The semantics of captured variables should be like function parameters, where they aren't necessarily a copy, but they're still readonly, and they're not observably of pointer type. This may already be how it works, but it's not documented that way, as far as I can tell.
This applies anywhere a |x| capture can be, such as: if (maybe) |x|, switch (union_enum) { Tag => |x|, etc.
The text was updated successfully, but these errors were encountered:
This is solved in my copy elision branch (#1682). The for elements appear to be const values but they are actually "views" into the array, no actual copy. Same with the other constructs. I consider this a duplicate of #287.
What if you want to be able to mutate the elements of an array? Is there a way to show that?
For instance suppose I use an array to implement a hashtable or sorted binary tree of pointers. If I remove an element, I want to put a marker value there or slide all the other elements down over the "hole".
You could construct a new array with all the values except the one to be removed, as for good functional programming style. But then you need to allocate and replace.
In this example,
row
with type[0x1000]u32
is possibly a copy of the row from the matrix. This would be improved by capturing|*row|
instead, but nowrow
can mutate thematrix
, which isn't what I want.The semantics of captured variables should be like function parameters, where they aren't necessarily a copy, but they're still readonly, and they're not observably of pointer type. This may already be how it works, but it's not documented that way, as far as I can tell.
This applies anywhere a
|x|
capture can be, such as:if (maybe) |x|
,switch (union_enum) { Tag => |x|
, etc.The text was updated successfully, but these errors were encountered: