I'm interested in metaprogramming examples written in F# that demonstrate the use of F#-specific functionality such as the TryGetReflectedDefinition
function in the F# standard library.
Where might I find such examples?
MetaProgramming gives Ruby the ability to open and modify classes, create methods on the fly and much more. A few examples of metaprogramming in Ruby are: Adding a new method to Ruby's native classes or to classes that have been declared beforehand. Using send to invoke a method by name programmatically.
Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage is called reflection.
Meta-Programming uses a program as a data type to generate code; Macros and Reflection are techniques of Meta-Programming in some sense.
We don't have many tools for metaprogramming in C. We don't have templates or constexprs C++ has; we don't have mixins like in D; and we certainly don't have homoiconicity of Lisp and Julia. All we have in C is the preprocessor that supports macros.
Some time ago, I wrote a project that translates F# quotations to GPU code using MSR Accelerator.
This is a fairly simple translator (compared e.g. to WebSharper, which is pretty complex), but it should demonstrate most of the features you're interested in. In particular, it translates functions marked with the ReflectedDefinition
attribute. It is also from your favorite scientific computing domain :-)
WebSharper and Unquote are two examples of open source F# libraries using Quotations (Unquote doesn't specifically use TryGetReflectedDefinition
, but I believe WebSharper does).
FSharp.PowerPack.Linq is another example. Also, I've found the implementation of quotations in the F# compiler to be a helpful and insightful reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With