6.14.1 Templated imports

Warning: This feature is experimental: it has known issues and its behavior may change in the future.

In Asymptote, it is possible to create modules that must have one or more types specified when they are imported. The first executable line of any such module must be of the form typedef import(<types>), where <types> is a list of required type parameters. For instance,

typedef import(T, S, Number);

could be the first line of a module that requires three type parameters. The remaining code in the module can then use T, S, and Number as types.

To import such a module, one must specify the types to be used. For instance, if the module above were named templatedModule, it could be accessed for types string, int[], and real with the import command

access templatedModule(T=string, S=int[], Number=real)
    as templatedModule_string_int_real;

Note that this is actually an access command rather than an import command, so the names of types, functions, etc. would have to be stated as e.g. templatedModule_string_int_real.Wrapper_Number rather than just Wrapper_Number (where Wrapper_Number is a type defined in templatedModule.asy).

Alternatively, the module could be imported via a command like

from templatedModule(T=string, S=int[], Number=real) access
    Wrapper_Number as Wrapper_real,
    operator ==;

This command would automatically rename Wrapper_Number to Wrapper_real and would also allow the use of any operator == overloads defined in the module.

For more information, see the examples in https://github.com/vectorgraphics/asymptote/tree/647b6c5732ec94a48f0f0b2446f02c86888fe7e7/tests/template.

Issues: Certain standard features of almost any type (such as ==, new, and the ability to call static methods on the type) may only be available for type arguments that are builtin or defined in the plain module.