FSharp.Quotations.Compiler


Tutorial

This library use easily.

In order to use this library, open FSharp.Quotations.Compiler namespace.

1: 
open FSharp.Quotations.Compiler

Typed Expression Tree

Suppose that you want to execute the following expression tree.

1: 
2: 
3: 
let x = 10
let y = 20
let expr = <@ x + y @>

You just use Execute method.

1: 
printfn "%d + %d = %d" x y (expr.Execute())

This code results as follows:

10 + 20 = 30

If you want to compile the expression tree and execute it later, you use Compile method.

1: 
let result = expr.Compile()

Or you can use ExprCompiler.compile function as same as using Compile method.

1: 
let result2 = expr |> ExprCompiler.compile

In order to execute result, you use ExecuteCompiledCode method.

1: 
printfn "%d + %d = %d" x y (result.ExecuteCompiledCode())

Untyped Expression Tree

This library does not support the untyped expression tree directly. If you want to execute untyped expression tree then use Expr.Cast method. It is the method of the F# standard library.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
open Microsoft.FSharp.Quotations

// helper function for casting
let cast<'T> (expr: Expr) : Expr<'T> = expr |> Expr.Cast

let untypedExpr = <@@ x + y @@>
let casted = untypedExpr |> cast<int>
printfn "%d + %d = %d" x y (casted.Execute())
namespace FSharp
namespace FSharp.Quotations
namespace FSharp.Quotations.Compiler
val x : int

Full name: Tutorial.x
val y : int

Full name: Tutorial.y
val expr : Quotations.Expr<int>

Full name: Tutorial.expr
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
member Quotations.Expr.Execute : unit -> 'T
val result : ICompiledCode<int>

Full name: Tutorial.result
member Quotations.Expr.Compile : unit -> ICompiledCode<'T>
val result2 : ICompiledCode<int>

Full name: Tutorial.result2
module ExprCompiler

from FSharp.Quotations.Compiler
val compile : expr:Quotations.Expr<'T> -> ICompiledCode<'T>

Full name: FSharp.Quotations.Compiler.ExprCompiler.compile
abstract member ICompiledCode.ExecuteCompiledCode : unit -> 'T
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Quotations
val cast : expr:Expr -> Expr<'T>

Full name: Tutorial.cast
val expr : Expr
Multiple items
type Expr =
  override Equals : obj:obj -> bool
  member GetFreeVars : unit -> seq<Var>
  member Substitute : substitution:(Var -> Expr option) -> Expr
  member ToString : full:bool -> string
  member CustomAttributes : Expr list
  member Type : Type
  static member AddressOf : target:Expr -> Expr
  static member AddressSet : target:Expr * value:Expr -> Expr
  static member Application : functionExpr:Expr * argument:Expr -> Expr
  static member Applications : functionExpr:Expr * arguments:Expr list list -> Expr
  ...

Full name: Microsoft.FSharp.Quotations.Expr

--------------------
type Expr<'T> =
  inherit Expr
  member Raw : Expr

Full name: Microsoft.FSharp.Quotations.Expr<_>
static member Expr.Cast : source:Expr -> Expr<'T>
val untypedExpr : Expr

Full name: Tutorial.untypedExpr
val casted : Expr<int>

Full name: Tutorial.casted
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
member Expr.Execute : unit -> 'T
Fork me on GitHub