Skip to main content

expr

Macro expr 

Source
macro_rules! expr {
    (true) => { ... };
    (false) => { ... };
    ($k:tt -> $v:tt) => { ... };
    (:: $name:ident [ $($args:tt)* ]) => { ... };
    (:: $d:tt $name:ident [ $($args:tt)* ]) => { ... };
    (:: $name:ident) => { ... };
    (:: $d:tt $name:ident) => { ... };
    ($head:ident $(:: $seg:ident)+ [ $($args:tt)* ]) => { ... };
    ($head:ident [ $($args:tt)* ]) => { ... };
    ({ $($assoc:tt)* }) => { ... };
    ($head:ident $(:: $seg:ident)+) => { ... };
    ($e:expr) => { ... };
}
Expand description

Build a Wolfram Language Expr with WL-like syntax.

§Syntax

PatternResult
expr!(System::Head[a, b])Normal with the symbol System`Head
expr!(A::B::C[a, b])Normal with A`B`C (each :: → a context backtick)
expr!(var[a, b])Normal with the variable var as head (call over var)
expr!(A::B::C)the symbol A`B`C
expr!(::Name) / expr!(::Name[…])the context-less symbol Name (works nested in arg, splice, and association-value positions too)
expr!(::$Name) / expr!(::$Name[…])the context-less $-symbol $Name (e.g. $Context, $InputFileName)
expr!(k -> v)Rule[k, v] — usable inline inside …[...]
expr!({k -> v, ...})Association
expr!(true) / expr!(false)True / False symbols
expr!("str"), expr!(42), expr!(3.14)string / integer / real
expr!(rust_var), expr!((rust_expr))Expr::from(…)
expr!(f[..iter])splice a sequence of Into<Expr> items as args

§Conventions

  • Symbols are always fully qualified: a bare ident is always a Rust variable (in head and arg position) — there is no implicit System`` prefix. Write System::Times[…], Global::x`, etc. for symbols.
  • Arg position: string literals become WL strings; k -> v becomes Rule[k, v] inline; {k -> v} an Association; (rust_expr) an arbitrary Rust expression; ..iter splices a sequence.
  • Nesting: Head[a, b] in arg position recurses to all depths.

§Examples

let msg = "something went wrong";
let e = expr!(System::Failure["RustPanic", {"MessageTemplate" -> msg}]);
let list = expr!(System::List[1, 2, 3]);
let table = expr!(Tabular::Arrow::ToTabular[list]);
let head = Symbol::new("Global`f");
let call = expr!(head[1, 2]);   // a variable head — a call over `head`