Evaluating a units of measure expression using quotations and the F# Powerpack CodeDOM
Does anyone know if it's possible to send an f# quotation expression that includes a (unit of measure) to the F# CodeDOM (provided in the codeplex powerpack) to be开发者_StackOverflow compiled on the fly?
The scenario that I'm trying to achieve is to have a runtime component that allows me to type a simple expression into some textbox that would then be sent off to the CodeDOM to be compiled or maybe straight to Fsc.exe. Overall I'd like to leverage the F# compiler at runtime to compile a snippet of code (like I can in C# with the C# CodeDOM provider) in order to leverage the units of measure feature. Is this sort of thing possible?
The CodeDOM provider in F# PowerPack compiles the generated source code using the F# compiler, so it supports the whole F# language including units of measure. CodeDOM objects used to represent code are quite limited and they don't have a way of representing units of measures directly. However, you can use CodeSnippetExpression, which allows you to use any text (string) as part of the CodeDOM tree (and the generator will simply insert the text to the generated source code).
- This should be simple to use (in fact, you could just pass the text entered by user to CodeDom either using
CodeSnippetExpression
or using similar type (that allows entire statements/types/etc.) - The compilation is done by calling
fsc.exe
, which means that users need to install F# compiler and the application needs rights to run other applications.
You also mentioned quotations in your question. Quotations are a representation of F# source code, but there is no built-in parser that would create quotation from a string (entered by user). If you created such parser (for the strings your users are going to enter), then you can create quotation tree and compile it using quotation compiler PowerPack.
- The compilation would more efficient, because quotations are compiled (using LINQ) in memory
- The compilation supports only a limited sub-set of the full F# language
- You would have to write your own parser to generate quotations from user input.
精彩评论