2 ms·
First I just want to point out that you're not allocating on the heap just by using references. This: let x = &1; ...is just sugar for: let x1 = 1; // a
by kibwen 13y ago
First I just want to point out that you're not allocating on the heap just by using references. This:
let x = &1;
...is just sugar for:
let x1 = 1; // allocated on the stack
let x = &x1; // reference to a stack allocation
Currently you only allocate on the heap when you use either ~ or @ pointers. And if you're writing an API for widespread consumption, then writing functions that take arguments by reference is actually encouraged since it gives the caller the maximum amount of flexibility.
As for the pain of refactoring your code to move from heap-allocations to stack-allocations, this is exactly why we're so fervent to encourage people to use stack-allocated values from the get-go. :) We're fans of Python's philosophy of making things explicit, which explains why we're different from C++ in this particular detail (i.e. we make you explicitly acknowledge when you're passing a reference to a function that expects a reference).