Movatterモバイル変換


[0]ホーム

URL:


GitHub

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:

macro inline(ex)    esc(isa(ex, Expr) ? pushmeta!(ex, :inline) : ex)end

Here,ex is expected to be an expression defining a function. A statement like this:

@inline function myfunction(x)    x*(x+3)end

gets turned into an expression like this:

quote    function myfunction(x)        Expr(:meta, :inline)        x*(x+3)    endend

Base.pushmeta!(ex, tag::Union{Symbol,Expr}) appends:tag to the end of the:meta expression, creating a new:meta expression if necessary.

To use the metadata, you have to parse these:meta expressions. If your implementation can be performed within Julia,Base.popmeta! is very handy:Base.popmeta!(body, :symbol) will scan a functionbody expression (one without the function signature) for the first:meta expression containing:symbol, 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, theargs array will be empty.

Not yet provided is a convenient infrastructure for parsing:meta expressions from C++.

Settings


This document was generated withDocumenter.jl version 1.8.0 onWednesday 9 July 2025. Using Julia version 1.11.6.


[8]ページ先頭

©2009-2025 Movatter.jp