Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Loading quotation data from an assembly - the explicitlyRegisterTopDefs function

I would like to understand how to retrieve the quotation from a top level function marked with [<ReflectedDefinition>] from an assembly.

It looks like this was done here: Tomas Petricek's blog: Quotation Visualiser Reloaded, but the code (at the very end of the article) makes a simple call to explicitlyRegisterTopDefs to retrieve the top level quoted definition.

I cannot seem to find this function in the latest version of the PowerPack or the F# compiler (I am working with .Net 4.0).

Lots of things happened to have changed since 2006 when the article was written, for example, the Microsoft.FSharp.Quotations.Raw was refactored, as you can see here.

Does anyone know how to capture these top level quotations with the latest versions of the PowerPack / compiler?

Thanks.

like image 487
Ncc Avatar asked Sep 16 '25 13:09

Ncc


1 Answers

We did a lot of stuff like this WebSharper. Basically you do (no powerpack needed):

module QP = Quotations.Patterns
module QDP = Quotations.DerivedPatterns

[<ReflectedDefinition>]
let myFunc x = x + 1

match <@ myFunc 1 @> with
| QP.Call(_, QDP.MethodWithReflectedDefinition d, _) ->
    printfn "%A" d
| _ ->
    printfn "ERROR"

I hope this helps with your scenario.

Note however that it has a ton of problems. Most grievous is that these active patterns throw exceptions from time to time. In addition, they are based on System.Reflection which slows things down enormously. Also, you have to account for unexpected things, like quotation currying not being resolved for you, certain constructor quotations failing, and so on.

For the upcoming WebSharper 2.4 I ended up rewriting the quotation loading code from scratch, using F# compiler sources as the definition of the binary format and avoiding System.Reflection, with great improvements in speed and reliability.

like image 123
t0yv0 Avatar answered Sep 18 '25 10:09

t0yv0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!