Walter suggested that I create a thread for an idea I've had for memory ownership & lifetimes.
The basic explanation is that every variable (including temporaries and returns) are always in reference to other variables. So that the order of destruction is correct and nothing escapes its owner (they can be bundled unless scope is involved).
These examples come from a talk with Timon a couple of days ago on Discord. His existing dislike for this idea is that it conflates different levels of indirection.
My worked example:
int[] array = [1, 2, 3]; // ownership: []
// arg ownership: [] (not ref/out)
// return ownership: [first argument] -> [array]
int* got = func(array); // ownership: [array]
int* func(int[] source) {
int[] slice = source[1 .. 2]; // ownership: [source]
int* ptr = &slice[0]; // ownership: [slice, source] -> [source]
return ptr; // ownership: [ptr] -> [source]
}
A question Timon came up with that I answered:
int[][] func(int[] a, int[] b){
return [a,b];
}
(for return value): // ownership: [first argument, second argument]
This ticks a lot of the boxes I'm using as preferable acceptance criteria:
- Inferred for anything non-virtual
- Nothing outlives its owner
- Fairly straight forward to understand