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

Why is call of operator==(point, null) ambiguous? #322

Closed jamadagni closed 2 years ago

jamadagni commented 2 years ago
import geometry;
point a = null;
if (a == null) write("yes"); else write("no");

I get:

-: 1.7: call of function 'operator ==(point, null)' is ambiguous

A point is just another struct right? When the following code is valid:

struct INT { int i; }
INT q = null;
if (q == null) write("yes"); else write("no");

and gives the (expected) output “yes”, I don't see why the same doesn't work for a point? Since the interpreter doesn't give clarity in the error message on what are the ambiguous candidates it is not easy for me to find what is wrong with operator ==(point, null).

To detect an uninitialized input point in a function, I found that alias(p, null) is the only way currently. See this post in the SF.net forum.

It would be good if operator ==(struct, null) worked uniformly for all structs to detect uninitialized inputs, while one realizes that for non-struct types (which apparently includes pairs) this is not possible or only possible with more difficulty in the implementation.

johncbowman commented 2 years ago

In commit cb6aeda7ff3c1ed42bb04ed45ba26ce5061797f1 I've added a diagnostic to list ambiguous functions.

The error mesage for this case sheds light on the underlying issue, which has to do with the heavy use of casting in the contributed module geometry.asy that you are using:

-: 1.33: call of function 'operator ==(point, null)' is ambiguous:

bool(mass a, mass b) bool[](pair a, pair[] b)

Using alias(a,null) is a good solution.