Quasi Quotes

Quasi quotes are syntactically supported by block tokens [| ... |] . All code inside of quasi quotes is not executed directly but is instead returned as an object graph representing the quoted code, instead. Quasi quotes can be constructed with the same string interpolation rules as strings are constructed. The resulting object graph can be programmatically manipulated and compiled into functions. This is especially powerful when combined with pattern matching.

var q = [| (x, y) -> x + y |] // quasi quoted function

// q is an object tree representing the above function
var f = q.compile()

printf(f(1, 2)) // 3

var x = {
  type = "Binary",
  op = "multiply",
  left = {
    type = "Reference",
    name = "x",
  },
  right = {
    type = "Constant",
    value = 2,
  }
}

var q2 = [| (x, y) -> $x + y |] // quasi quotes with interpolation for x
var f2 = q2.compile()

print(f2(3, 4)) // 3*2+4=10

Last edited Nov 24, 2012 at 12:06 AM by justinc, version 1

Comments

No comments yet.