Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

captured values should have the same copy/const semantics as function parameters #1766

Closed
thejoshwolfe opened this issue Nov 21, 2018 · 2 comments
Milestone

Comments

@thejoshwolfe
Copy link
Contributor

thejoshwolfe commented Nov 21, 2018

var matrix = [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.

@andrewrk
Copy link
Member

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.

@andrewrk andrewrk added this to the 0.4.0 milestone Nov 21, 2018
@kyle-github
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants