vectorgraphics / asymptote

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

Function overloading casuses keyword conflict #349

Closed avidseeker closed 1 year ago

avidseeker commented 1 year ago
real f(real keyword m) {return m*2;}
real f(real keyword n) {return n;}

write(f(m=2.0), f(n=3.0), file=stdout);

results in

write(f(m=2.0), f(n=3.0), file=stdout);
       ^
cannot call 'real f(real keyword n)' with parameter '(real m)'

despite having m,n as mandatory keyword parameters.

johncbowman commented 1 year ago

You misunderstand the use of keyword. First consider this example:

real f(real m) {return m*2;}
real f(real n) {return n;}
write(f(2.0), f(3.0), file=stdout);

The second function has the same signature as the first and therefore overrides (replaces) it. Adding keyword to these signatures forces the user to explicitly specify the argument name (in this case n, not m). Just like in mathematics, if you want to use two different functions having the same signature, you will need to distinguish them by giving them different names (like f and g).