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

Allow aliases with `using` #407

Open charlesstaats opened 9 months ago

charlesstaats commented 9 months ago

C++11 introduced the keyword using, which can be used for a more intuitive but equivalent version of typedef. For instance,

typedef int intop(int);
using intop = int(int);

are now equivalent. This PR introduces a similar feature into Asymptote. There's no new functionality, but many programmers find the using-style alias easier to read since it is much more similar to variable assignment.

charlesstaats commented 9 months ago

Correction: The word using was already present in C++ (as in using namespace std;); it was not introduced in C++11, only given an additional use. I think it's unlikely that existing users have variables, functions, or types called using, but I'd be open to repurposing an existing keyword instead if there is one that would work.

Additional note: The way I've implemented using is a bit hacky. In some sense, I think this is unavoidable: the tradeoff for a very elegant implementation was the weird syntax that typedef uses to define function types. I welcome suggestions on how to make the implementation more elegant and/or more thorough (e.g., supporting comma-separated aliases rather than only the single alias, or refactoring the typedef definition rather than clunkily rewriting a using statement into a typedef). But I also urge the reviewers not to make the perfect the enemy of the good.

charlesstaats commented 2 months ago

Upon further consideration, I actually think the current approach may be the best "reasonable" option available. I have added comments to camp.y to explain how the syntax of using is designed to imitate the type alias syntax in the C++ standard.