On Wednesday, 9 September 2015 at 20:18:57 UTC, Paul wrote:

Is it possible to call a function like this...

void foo(ref int[] anArray)

...with slices of static arrays? I thought I might be able to use [0..$-1] but to no avail - I get an error like this (which is confusing!):
Note that the upper bound of the slice is exclusive - [0..$-1] is not the whole array but everthing except the last entry. To slice the whole array, you can use [0..$] or just write
int[42] a;
auto b = a[];


(ref int[] anArray) is not callable using argument types (int[])

I've modified the function declaration and used .ptr when calling it but it seems like a retrograde step for a modern language - although I suppose ref is pretty much the same thing.

In practice I'm using arrays of structs (which are of various fixed sizes and known at compile time) that are currently global in scope - something that I'd like to avoid.

I suppose I could just ask, what is the 'best' way to give access to variously sized static arrays between different modules?

It is impossible to pass a rvalue as a reference (intended behaviour, afaik it is causes problems if the reference escapes from foo and outlasts the rvalue). There is a bug report about the strange error message somewhere in the bug tracker. Some time ago someone announced to implement passing rvalue as reference but it seems the PR was not merged for dmd 2.068.

Workaround:
use a temporary variable.
instead of

int[42] a;
foo(a[]);

write

int[42] a;
auto tmp = a[];
foo(tmp);

Reply via email to