On Saturday, 12 September 2020 at 03:11:09 UTC, Ali Çehreli wrote:
On 9/11/20 6:44 PM, mw wrote:> e.g.
>
> int[] a = new int[la];
> int[] b = new int[lb];
> int[] c = new int[lc];
> int[] d = new int[ld];
>
>
> the func I want to write, e.g. for 2 arrays (instantiation)
is like this:
>
> void print_random_elem_addr(int[] x, int[] y) {
>    auto i = random_int_between(0, x.length);
>    auto j = random_int_between(0, y.length);
>    print(&(x[i], &(y[j]));  // only single print() func call
allowed!
> }
>
>
> But I want one generic function, which can be called as:
>
> print_random_elem_addr(a, b);
> print_random_elem_addr(a, b, c);
> print_random_elem_addr(a, b, c, d);

Thanks for the reply.

If they are all of same type like int[] in this case, then you

but, this is not the intention, we should suppose the array's are heterogeneous type ...

can variable number of parameters, which means "any number of int[] arrays" below, elements of which can be called either as separate arguments or as a single array argument:

import std.stdio;
import std.random;

void print_random_elem_addr(int[][] arrays...) {

... to prevent passing in parameters as array of array like this.

  foreach (i, array; arrays) {
    const chosen = uniform(0, array.length);
writefln!"Array %s, element %s: %s"(i, chosen, &array[chosen]);

actually this writefln will be called n times.

I intentionally require:

print(&(x[i], &(y[j])); // only single print() func call allowed!


I.e. I want to learn the generic meta-programming way to assemble such parameter list (&(x[i], &(y[j])) at compile time, it is possible?


Reply via email to