Idea: Ownership & lifetime escape analysis by variables in reference to
rikki cattermole
rikki at cattermole.co.nz
Sun May 29 08:03:57 UTC 2022
On 29/05/2022 7:42 PM, Ola Fosheim Grøstad wrote:
> Maybe look at the basic ideas for static analysis? Then you will know
> the limitations.
>
> How do you deal with conditionals?
```d
Thing func(Thing a, Thing b, bool pickB) {
return pickB ? a : b; // ownership: [a, b]
}
```
> How do you deal with mutable arrays of pointers?
```d
void func(Thing a, Thing b) {
int[] arr; // ownership: []
arr ~= a; // arr ownership: [a]
arr ~= b; // arr ownership: [a, b]
arr = arr[0 .. 1]; // arr ownership: [a, b]
arr ~= b; // arr ownership: [a, b]
}
```
> Simple stuff like a queue of size two? How do you know what it contains?
See above
> With lifetimes you can be pessimitic: max lifetime of anything that
> could in theory be put into it unless I am able to prove otherwise. Then
> you dont have to try to prove something unless it is needed.
>
> Better to just add ARC and do all these things as optimizations.
I too would like to have proper reference counting on structs and
classes with optimizations, but the compiler has to know what object to
do the reference counting on! Right now things can escape reference
counting.
More information about the Digitalmars-d
mailing list