The D operator xlate is used to perform a
translation from an input expression to one of the defined
translation output structures. The xlate
operator is used in an expression of the form:
xlate <output-type> (input-expression )
For example, to invoke the hypothetical translator for
FILE structs defined above
and access the file_fd member, you would write
the expression:
xlate <struct file_info *>(f)->file_fd;
where f is a D variable of type FILE *. The
xlate expression itself is assigned the type defined by the
output-type. Once a translator is defined, it can be used to
translate input expressions to either the translator output struct type, or
to a pointer to that struct.
If you translate an input expression to a
struct, you can either dereference a particular
member of the output immediately using the
“.” operator, or you can assign the entire
translated struct to another D variable to make
a copy of the values of all the members. If you dereference a
single member, the D compiler will only generate code
corresponding to the expression for that member. You may not apply
the & operator to a translated
struct to obtain its address, as the data
object itself does not exist until it is copied or one of its
members is referenced.
If you translate an input expression to a pointer to a
struct, you can either dereference a particular
member of the output immediately using the ->
operator, or you can dereference the pointer using the unary
* operator, in which case the result behaves as
if you translated the expression to a struct.
If you dereference a single member, the D compiler will only
generate code corresponding to the expression for that member. You
may not assign a translated pointer to another D variable as the
data object itself does not exist until it is copied or one of its
members is referenced, and therefore cannot be addressed.
A translator declaration may omit expressions for one or more
members of the output type. If an xlate
expression is used to access a member for which no translation
expression is defined, the D compiler will produce an appropriate
error message and abort the program compilation. If the entire
output type is copied by means of a structure assignment, any
members for which no translation expressions are defined will be
filled with zeroes.
In order to find a matching translator for an
xlate operation, the D compiler examines the
set of available translators in the following order:
The compiler looks for a translation from the exact input expression type to the exact output type.
The compiler resolves the input and output types by following
any typedef aliases to the underlying type
names, and then looks for a translation from the resolved
input type to the resolved output type.
The compiler looks for a translation from a compatible input type to the resolved output type. The compiler uses the same rules as it does for determining compatibility of function call arguments with function prototypes in order to determine if an input expression type is compatible with a translator's input type.
If no matching translator can be found according to these rules, the D compiler produces an appropriate error message and program compilation fails.