Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The borrowing scheme works for pointers that are passed in to functions, but not ones returned from functions As long as you don't go against the grain of the scope, you are fine. It is a natural and simple restriction that can be addressed by local data flow analysis.

The problem, as I see it, is going against the grain of the scope, a problem faced by iterators. For example, consider

    for (p = f(s); p; p = p.next) {} 
won't be allowed, since f may not be able to return a borrowed pointer (like s.ptr). Rust solves this using parametric lifetimes.

In general, I am saying that returning a non-owned pointer from a function is common, and required. Am I wrong in this assumption? If not, is there an alternate architecture to get around this problem? Note that marking the pointer as const is not an option.



The solution is to have the function not return a pointer but return a ref. A ref is a non-owned pointer.


Would this be similar to passing argument(s) and return pointers/variables to a C function - or similar with assembler, reserving some registers for return values (aka caller manages memory)?

But with the (d) compiler helping enforce correctness?


Not sure what you mean, but sounds like it is.


Thanks. References work for the iterator example.

I am a bit uncomfortable with not being able to move a pointer out of a structure, but it may be a restriction that's worth it; I can't think of a counterexample off the top of my head.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: