Decomposing quotation expressions gives you a way to inspect and transform ASTs, but you can build up quotation expressions dynamically at runtime as well.
Generating quotation expressions is especially useful when creating mini-programming languages. Rather than writing F# code and processing the AST, you can parse another, perhaps simpler, language to generate the expression tree and then process that.
Any active pattern you see for decomposing quotation expressions has
a complementary static method on the
Expr class for producing an equivalent
The following snippet shows taking the quotation of a simple expression and the equivalent way to build up that same expression value:
let organicQuotation = <@ let x = (1, 2, 3) (x, x) @> let syntheticQuotation =
Expr.Let( new Var("x", typeof<int * int * int>),
Expr.Value(3) ] ),
Expr.GlobalVar("x").Raw ] ) )
Using methods on the
Expr type isn’t the only way to generate
quotation expressions. Quotations may contain
expression holes, which are locations for new
Expr<_> to be
placed, allowing you to build up expression trees by simply patching
holes in an existing expression.
To declare an expression hole, simply use a percent sign
% in front of any expression within the quotation, and the result of that expression will be bound to the generated quotation expression tree. (Similar ...