How to return multiple dynamic arrays

I need to define a function of the form

proc foo(args : type) : ([?D1] int, [?D2] int) {
  ...
  return (a1, a2);
}

Chapel complains about the return type. Removing the return type works. This is not great for readability. I tried using 2 out arguments of the form:

proc foo( args : type, out a1:[] int, out a2: [] int) {
...;
a1 = [i in D1] i;
a2 = [i in D2] i;
return;
}

but it complained about that too.
Is there a better way to achieve this? Thanks.
--shiv--

Hi @00shiv,

If I understand right, I think the following will work.

proc foo(D1, D2): ([D1] int, [D2] int) {
  var a = [i in D1] i;
  var b = [i in D2] i;
  return (a, b);
}

This assumes the domains are passed to the function. If they are computed in the function body, you can still specify the return type (as long as they are computable solely passed on the function formals). For example

proc getDom(low, high) {
  return {low..high};
}
proc bar(low, high): ([getDom(low, high)] int, [getDom(low, high)] int) {
  const D = getDom(low, high);
  var a = [i in D] i;
  var b = [i in D] i;
  return (a, b);
}

The query syntax you wrote in the OP isn't valid for a return type, but usually you can omit the domain expression for an array return type. For example the following works.

proc bar(low, high): [] int {
  const D = getDom(low, high);
  var a = [i in D] i;
  return a;
}

However, making it a tuple doesn't work (which might also be what you ran into)

proc bar(D1, D2): ([] int, [] int) {
  var a = [i in D1] i;
  var b = [i in D2] i;
  return (a, b);
}

I'm fairly sure this one not working is a bug and you should open a github issue for it if you need it.

If this is the kind of pattern you need to support, where the domain is not knowable by the function formals and you truly want/need ([] int, [] int), your best bet for the time being is to just omit the return type (and maybe have it as a comment since presumably you want it for a strict coding style?).

-Jade

Hi Jade,

Thanks for the quick reply. I cannot compute the domain based on the formals; they depend on the numerical values inside the array. And you are right: it is the tupled version that gave me problems. I will live with not specifying the return types for now as I have another bug to chase down first!

Thanks.

--shiv--

I opened [Bug]: Cannot use explicit return type for tuple of arrays · Issue #29235 · chapel-lang/chapel · GitHub for this. I hope to have a fix soon

-Jade

1 Like