Qit


QitBindingObj Type

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.

Example

 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
Multiple items
type Sum = new: unit -> Sum member Add: e: int -> unit member CurrentSum: unit -> int member SumExpr: 'a

--------------------
new: unit -> Sum
type ResizeArray<'T> = System.Collections.Generic.List<'T>
Multiple items
type ReflectedDefinitionAttribute = inherit Attribute new: unit -> ReflectedDefinitionAttribute + 1 overload member IncludeValue: bool

--------------------
new: unit -> ReflectedDefinitionAttribute
new: includeValue: bool -> ReflectedDefinitionAttribute
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

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

--------------------
type int<'Measure> = int
module Seq from Microsoft.FSharp.Collections
val reduce: reduction: ('T -> 'T -> 'T) -> source: 'T seq -> 'T
val a: Sum
member Sum.Add: e: int -> unit
val str: string
property System.String.Length: int with get
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
member Sum.CurrentSum: unit -> int
Here we define our 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
 @>
val str: string
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
property System.String.Length: int with get
Had we provided an overload to the Final method, we could have further transformed the expression.

Constructors

Constructor Description

QitBindingObj()

Full Usage: QitBindingObj()

Returns: QitBindingObj
Returns: QitBindingObj

Instance members

Instance member Description

this.Final

Full Usage: this.Final

Parameters:
Returns: Expr The transformed expression.
Modifiers: abstract

An abstract method that is intended to be overridden by derived types to perform a final transformation on an expression before it replaces the let-binding.

arg0 : Expr
Returns: Expr

The transformed expression.

this.Var

Full Usage: this.Var

A property that holds an optional variable which may be used by derived types to reference the variable captured by the let-binding.