A type that captures the variable to which it's let-bound within an expression. Typically, this type would be inherited from, and when let-bound in an expression,
the actual value would be evaluated and substituted in the body of the let-binding. The body is then expanded and passed to the Final
method,
where the result replaces the entire let-binding.
open Qit
type Sum() =
inherit QitBindingObj()
let exprsToSum = ResizeArray()
[<QitOp; ReflectedDefinition>]
member x.Add(e : int) =
splice (
exprsToSum.Add(<@e@>)
<@()@>
)
member x.SumExpr =
if exprsToSum.Count = 0 then
<@0@>
else
exprsToSum |> Seq.reduce (fun a b -> <@ !%a + !%b @>)
[<QitOp; ReflectedDefinition>]
member x.CurrentSum() = splice x.SumExpr
<@
let a = Sum()
a.Add(2)
let str = "my string"
a.Add(str.Length)
printfn "Current sum %d" (a.CurrentSum())
a.Add(5)
printfn "Current sum %d" (a.CurrentSum())
a.CurrentSum()
@>
|> Quote.expandOperators
|> Quote.evaluate
// Current sum 11
// Current sum 16
// val it: int = 16
Sum
type which inherits from QitBindingObj
. Methods and properties that are used within quotations typically have the QitOp
and ReflectedDefinition
attributes.
We use a separate SumExpr
property since it uses quotation operators internally, and we don't want to expand those within the CurrentSum
method, which is a QitOp
. This is because the QitOp
attribute
means Quote.expandOperators
will expand the call, and we don't want to expand the SumExpr
property call.
The resulting expression after expansion is similar to:
<@
let str = "my string"
printfn "Current sum %d" (2 + str.Length)
printfn "Current sum %d" (2 + str.Length + 5)
2 + str.Length + 5
@>
Final
method, we could have further transformed the expression.
Constructor | Description |
|
|
Instance member | Description |
|
|
Full Usage:
this.Var
|