Talking to the compiler (the :meta mechanism)¶
In some circumstances, one might wish to provide hints or instructions
that a given block of code has special properties: you might always
want to inline it, or you might want to turn on special compiler
optimization passes. Starting with version 0.4, Julia has a
convention that these instructions can be placed inside a :meta
expression, which is typically (but not necessarily) the first
expression in the body of a function.
:meta expressions are created with macros. As an example, consider
the implementation of the @inline macro:
macroinline(ex)esc(_inline(ex))end_inline(ex::Expr)=pushmeta!(ex,:inline)_inline(arg)=argHere, ex is expected to be an expression defining a function.
A statement like this:
@inlinefunction myfunction(x)x*(x+3)endgets turned into an expression like this:
quotefunction myfunction(x)Expr(:meta,:inline)x*(x+3)endendpushmeta!(ex,:symbol,args...) appends :symbol to the end of
the :meta expression, creating a new :meta expression if
necessary. If args is specified, a nested expression containing
:symbol and these arguments is appended instead, which can be used
to specify additional information.
To use the metadata, you have to parse these :meta expressions.
If your implementation can be performed within Julia, popmeta! is
very handy: popmeta!(body,:symbol) will scan a function body
expression (one without the function signature) for a :meta
expression, extract any arguments, and return a tuple (found::Bool,args::Array{Any}). If the metadata did not have any arguments, or
:symbol was not found, the args array will be empty.
Not yet provided is a convenient infrastructure for parsing :meta
expressions from C++.