Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed a variable inside an F# quotation

I'm writing an F# dsl for SQL (http://github.com/kolosy/furious).

A select statement would look like this:

type person = {
    personId: string
    firstname: string
    lastname: string
    homeAddress: address
    workAddress: address
    altAddresses: address seq
}
and address = {
    addressId: string
    street1: string
    zip: string
}

let (neighbor: person seq) = 
    db.Yield <@ Seq.filter (fun p -> p.homeAddress.zip = '60614') @>

The obvious (and silly) question is... How do I parametrize the quotation?

If I just somehting like:

let z = "60614"
let (neighbor: person seq) = 
    db.Yield <@ Seq.filter (fun p -> p.homeAddress.zip = z) @>

then z gets resolved into a static property accessor (PropertyGet(None, String z, [])). I need something that will let me retrieve the value of the variable/let binding based solely on the quotation. Ideas?

like image 344
kolosy Avatar asked Dec 28 '22 10:12

kolosy


1 Answers

Quotations are not my forte, but check out the difference here:

let z = "60614" 
let foo = <@ List.filter (fun s -> s = z) @> 
printfn "%A" foo

let foo2 = 
    let z = z
    <@ List.filter (fun s -> s = z) @> 
printfn "%A" foo2

I think maybe having 'z' be local to the expression means the value is captured, rather than a property reference.

like image 86
Brian Avatar answered Jan 04 '23 23:01

Brian