Well, for me it doesn't compile because expr is not defined, but I suppose the following is similar to what you're trying to do (and it throws the same error):

1
2
3
let f x y = <@ x + y + 1 @>
let v = 1
let l = <@ fun x -> % f x v @>

Note that <@ %f x v @> is equivalent to <@ %(f x v) @>. This means that you're trying to pass x, a variable created inside a quotation, to a spliced expression. This is not possible. The compiler cannot expand f x v into a quotation to be inserted into l, since x does not have a value yet.

Maybe the following is what you're trying to do:

1
2
3
let f = <@ fun x y -> x + y + 1 @>
let v = 1
let l = <@ fun x -> (%f) x v @>

Here, I'm passing x to f after it's been spliced, which means the call is entirely within the quotation: no expansion needs to be done during the construction of the quotation.

And in case expr was a quotation that you wanted spliced into l, then the following should do the trick:

1
2
3
let f expr = <@ fun x y -> x + y + 1 @>
let v = 1
let l = <@ fun x -> (%f expr) x v @>
By on 1/28/2013 1:06 AM ()

The code I provided is a simplified version of what I'm trying to write and in my case I have a function with a bunch of parameters, which returns a quoted expression.
Is it possible to work around the limitation you mentioned by construction the expression using the Expr API?

By on 1/28/2013 1:49 AM ()

Actually, if you try constructing the expression with the Expr API, you will see exactly why this limitation is there. You will end up with something like:

1
2
3
let l =
  let x = Var("x", typeof<int>)
  Expr.Lambda(x, f x)

where f x is supposed to use the integer value of x; but here x isn't an int, it's a Var that can only be used with Expr.Var(x).

So, depending on your use case, you need to either take the reference to x into the quotation (like I did in my previous suggestion) or take x out of the quotation entirely, like so:

1
let l x = <@ % f x v @>
By on 1/28/2013 3:21 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper