Use quotation

Category:
Quotation
Description: Basic quotation sample
Code:
let QuotationSample1() = 
let expr : Expr = <@ 1 + 1 @>
let expr2 : Expr = <@@ 1 + 1 @@>
printfn "expr type is %s" expr.Type.Name
printfn "expr2 type is %s" expr.Type.Name

Execution Result:
expr type is Int32
expr2 type is Int32

Use quotation to get function name and parameters

Category:
Quotation
Description: Use quotation to get function name and parameters
Code:
[<ReflectedDefinition>]
let myFunction p0 p2 =
printf "hello quotation"

let QuotationSample2() =
let rec get_parms exp =
match exp with
| Lambda (var,body) ->
let index = if (var.Name.IndexOf("@") - 1) < 0 then var.Name.Length-1 else (var.Name.IndexOf("@") - 1)
let name = [var.Name.[..index]]
let p = get_parms body
name @ p
| _ -> []

List.iter (printf "%A\r\n") (get_parms <@ myFunction @>)

Execution Result:
"p0"
"p2"

Last edited Sep 12, 2011 at 11:46 PM by ttliu2000, version 2

Comments

No comments yet.