The autounravel
modifier can be used to automatically unravel a field.
This is useful when building an associated library of functions
that operate on a structure. For instance, consider a simple
implementation of the rational
structure defined in rational.asy
:
struct rational { int p=0, q=1; void operator init(int p, int q) { this.p=p; this.q=q; } } rational operator +(rational a, rational b) { return rational(a.p*b.q+b.p*a.q, a.q*b.q); }
To allow rational
to be used as a type parameter for a templated import
that adds instances of rational
, we should move operator +
into the body of the rational
structure and add the
autounravel
modifier:
struct rational { int p=0, q=1; void operator init(int p, int q) { this.p=p; this.q=q; } autounravel rational operator +(rational a, rational b) { return rational(a.p*b.q+b.p*a.q, a.q*b.q); } }
This is almost equivalent to the previous code, but now the +
operator
will be accessible wherever the rational
structure is.
Currently, types cannot be autounraveled. Users who encounter a case where this might be useful can submit a feature request at https://github.com/vectorgraphics/asymptote/issues.