vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
550 stars 90 forks source link

Parameters of type pair with defaults cause unexpected type errors #381

Closed DanielW0108 closed 1 year ago

DanielW0108 commented 1 year ago

Minimal example:

int f (pair foo=(0,0), int x) {
  return x;
}

f(1);

Trying to compile this gives the following error: cannot call 'int f(pair foo=<default>, int x)' with parameter '(int)'

Calling f(x = 1) compiles, as does swapping the order of the parameters to this:

int f (int x, pair foo=(0,0)) {
  return x;
}

f(1);

This seems to happen whenever x has a type that could appear inside a pair. The same type error happens if int is swapped for real, but swapping it for string avoids it:

string f (pair foo=(0,0), string x) {
  return x;
}

f("a");

Casting to a non-pair-y type seems to behave differently to casting to a pair-y type:

This happens on both the web version (2.86-7) and the latest version available on my repo (2.70)

johncbowman commented 1 year ago

This is the expected behaviour of Asymptote's function signature matching algorithm, as described here:

https://asymptote.sourceforge.io/doc/Default-arguments.html

See also https://asymptote.sourceforge.io/doc/Named-arguments.html

In your example, the int argument can be cast to a pair, and so it assigned to the first argument. You then need to supply a second int since the second argument does not have a default value.

johncbowman commented 1 year ago

Perhaps what you really intended was

int f (explicit pair foo=(0,0), int x) {
  return x;
};
DanielW0108 commented 1 year ago

Ah, I wasn't aware that ints can be implicitly cast to pairs - makes sense when treating pairs as complex numbers, but unintuitive when they're mostly being used as coordinates. Thanks for the response.