-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Proposal: Improving Ptrs and Ownership #885
Comments
Sorry if this is off topic but there are 4 or 5 issues about ownership and this looked like the most recent open. I was wondering if there wound be a way to make returns from functions as transferring ownership in the same way the errors are returned and the compiler enforces that the caller handles it? Even if it means ignoring or transferring the ownership somewhere else. // syntax is total rubbish made up but % means returns ownership
pub fn alloc(size: size_t) %&void { return malloc(size); } // assumes no error or null in this bad example
pub fn bar() %&void {
return alloc(9); // okay, ownership is returned..
}
...
var ptr = alloc(64); // error ownership not handled...
var ptr = alloc(64) clean defer |p| free(p); // ok you defered the clean up
var ptr = alloc(64) clean defer |p| {}; // ok you defered the clean up but yeah.. totally leaking bub
// This idea can also work for other resources
struct File {
... data
pub fn open(name: []u8) !%File {
return File { ... };
}
pub fn close(self: &Self) ...
}
var myfile = File.open("passwords.txt") catch |e| { warn("Crap!"); return false; } clean defer |fp| fp.close(); I think the next step would be to transfer ownership as arguments:
The last part of the puzzle is:
If it is case 1 then the compiler stops enforcing ownership rules but in case 2 we can add more safty checks..
Of course these means the ownership would have to be nullable, or if we want to keep nullable separate then myfile would have to be nullable as well.
You should be able to barrow from an owned:
One thing I am not sure about at all is if you transfer ownership var tmp = myfile; Should you have to include a clean defer every time?
This could give us basic compiler enforced ownership without crazy lifetime analysis or forcing everything to use the ownership system. Sorry for the long rant... |
The clean defer stuff looks like #782. As for the original proposal, it's not really clear to me how it works or what the benefits are, and looking at it, I'm not inspired to do anything with it. |
Okay currently pointers that aren't 'local' or stack based seem to be unconventional. I think Zig could benefit from a 'smart pointer' like system (that doesn't go overboard of course). I have only coded some basic things in Zig but due to its similarity the code should be correct :).
Std way
Now of course a lot of memory issues come from this being used badly so adding a few ownership tools could help the first being a struct ownership tag.
Furthermore something like the idea of a unique ptr which would just be like this;
Which is actually quite nice to be honest. This does prevent copying just so its close to the spec of C++, however I would say that we don't need to prevent it as this is more around just who owns the ptr then anything.
I feel the best way to get involved is jumping in the deep end so hopefully I got most things right :).
The text was updated successfully, but these errors were encountered: