Repeated query of size of type in argument list

Hi @damianmoz - I think that you are encountering buggy behavior on the part of the compiler here -- in particular, I don't think it works today to use both the tuple unpacking formal and the type query. I've gone ahead and created issue Problem combining tuple unpacking formal and type queries · Issue #23302 · chapel-lang/chapel · GitHub to serve as a bug report.

Here's a little investigation of several ways to write this; the first is what you had (and doesn't work); the second is another option, but also doesn't work. The third can serve as a workaround until the others work.

use Math;

//inline proc cmplx1((x , y) : (real(?w), imag(?w)))
//{
//  writeln(w); // error: w undeclared [should be an error about w being redeclared]
//  return x * exp(y);
//}

// error: unable to resolve type
//inline proc cmplx((x , y)) where isRealType(x.type) && isImagType(y.type)
//{
//    return x * exp(y);
//}

// this version seems to work
// note: you could add `numBits(tup(0).type) == numBits(tup(1).type)`
//       if you want the real and imag to be the same width.
inline proc cmplx(tup)
  where isTuple(tup) && isRealType(tup(0).type) && isImagType(tup(1).type)
{
  const (x,y) = tup;
  return x * exp(y);
}

writeln(cmplx( (1.0, 40.0i) ));
1 Like