Proper way to accept either static or dynamic array as a parameter
jfondren
julian.fondren at gmail.com
Sun Sep 12 02:49:48 UTC 2021
On Sunday, 12 September 2021 at 02:44:36 UTC, Alex Bryan wrote:
> `T[] dynArr` can be passed (by reference) to a function that
> takes `ref T[] data` but `T[10] data` cannot? Why not?
```d
void add1(ref int[] nums) {
nums ~= 1;
}
unittest {
int[] nums;
nums.add1;
nums.add1;
nums.add1;
assert(nums == [1, 1, 1]);
}
```
the `ref` allows `add1` to extend the length of the slice passed
to it, with potential reallocation. This doesn't make sense with
a static array whose length can't be modified.
More information about the Digitalmars-d-learn
mailing list