チュートリアル
このライブラリは簡単に使えます。
このライブラリを使うためには、FSharp.Quotations.Compiler
名前空間をオープンしておくといいでしょう。
1:
|
open FSharp.Quotations.Compiler |
型付きの式木
次のような式木を実行したい場合を考えてみます。
1: 2: 3: |
let x = 10 let y = 20 let expr = <@ x + y @> |
Execute
メソッドを使うだけでできます。
1:
|
printfn "%d + %d = %d" x y (expr.Execute()) |
このコードを実行すると以下のように出力されます:
|
もし式木をコンパイルして、あとで実行したい場合は、Compile
メソッドを使います。
1:
|
let result = expr.Compile() |
Compile
メソッドを使うのと同じ意味でExprCompiler.compile
関数も使えます。
1:
|
let result2 = expr |> ExprCompiler.compile |
result
を実行するためには、ExecuteCompiledCode
メソッドを使います。
1:
|
printfn "%d + %d = %d" x y (result.ExecuteCompiledCode()) |
型なしの式木
このライブラリは、型なしの式木を直接はサポートしていません。
もし型なしの式木を実行したい場合は、Expr.Cast
メソッドを使います。
このメソッドはF#の標準ライブラリのメソッドです。
1: 2: 3: 4: 5: 6: 7: 8: |
open Microsoft.FSharp.Quotations // キャストのためのヘルパ関数 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
Full name: Tutorial.x
val y : int
Full name: Tutorial.y
Full name: Tutorial.y
val expr : Quotations.Expr<int>
Full name: Tutorial.expr
Full name: Tutorial.expr
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
member Quotations.Expr.Execute : unit -> 'T
val result : ICompiledCode<int>
Full name: Tutorial.result
Full name: Tutorial.result
member Quotations.Expr.Compile : unit -> ICompiledCode<'T>
val result2 : ICompiledCode<int>
Full name: Tutorial.result2
Full name: Tutorial.result2
module ExprCompiler
from FSharp.Quotations.Compiler
from FSharp.Quotations.Compiler
val compile : expr:Quotations.Expr<'T> -> ICompiledCode<'T>
Full name: FSharp.Quotations.Compiler.ExprCompiler.compile
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
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<_>
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
Full name: Tutorial.untypedExpr
val casted : Expr<int>
Full name: Tutorial.casted
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<_>
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