robkuz
robkuz

Reputation: 9934

Building Code Expressions "Manually"

I want to construct a F# code expression. However I dont want to do that via code quotations but construct it via objects. However the documentation on how to do that is pretty scarce.

Given the following snippet

type Foo = { foo = string}
let bar = {foo = "foo"}
let ex = <@ bar.foo @>

this yields for ex.

PropertyGet (Some (ValueWithName ({foo = "foo";}, bar)), foo, [])

But I simply cant replicate that exact term above. For example I dont find anything on the constructor ValueWithName.

So how can I

<@ bar.foo @> = PropertyGet (Some (ValueWithName ({foo = "foo";}, bar)), foo, [])

Upvotes: 2

Views: 129

Answers (1)

kvb
kvb

Reputation: 55195

Using the library to construct quotations manually is quite difficult to do correctly, so I'd highly recommend using quotation literals wherever you can. However, here's how to achieve your example if you want to:

open Microsoft.FSharp.Quotations

Expr.PropertyGet(
    Expr.ValueWithName({ foo = "foo" }, "bar"), 
    typeof<Foo>.GetProperty("foo"))

Upvotes: 4

Related Questions