13

Is there an F# equivalent to eval? My intent is to have my app load a small code sample from a file and essentially

let file = "c:\mysample"
let sample = loadFromFile file
let results = eval(sample)

I am new to F# and trying to figure out some of the limitations before I apply it to a project.

Thank you

p.campbell
  • 98,673
  • 67
  • 256
  • 322
akaphenom
  • 6,728
  • 10
  • 59
  • 109
  • 1
    possible duplicate of http://stackoverflow.com/questions/372043/how-can-evaluate-an-expression-stored-as-a-string-in-f – Brian Apr 09 '10 at 14:12
  • Do you want to load F# code from that file? – phimuemue Apr 09 '10 at 14:18
  • I think this post asks a bit different question (although technically related). The one mentioned by Brian asks about simple expressions, while this one asks about compiling/evaluating a file. Technically, this may be the same thing, but it is probably quite different use case. – Tomas Petricek Apr 09 '10 at 14:40

1 Answers1

13

There is no function that would allow you to do this directly. However, when you want to compile an F# source file programatically, you can invoke the F# compiler from your application. The easiest way to do this is to use F# CodeDOM provider, which is available as part of the F# PowerPack (in the FSharp.Compiler.CodeDom.dll assembly). However, note that you need to have the F# compiler installed on the user's machine.

The code to run the compiler would look roughly like this:

open Microsoft.FSharp.Compiler.CodeDom

let provider = new FSharpCodeProvider()
let compiler = provider.CreateCompiler()

let parameters = new CompilerParameters
  (GenerateExecutable = true, OutputAssembly = "file.exe")
let results = icc.CompileAssemblyFromFile(parameters, "filename.fs")

Once you compile the source file into an assembly, you'll need to load the assembly using .NET Reflection and run some specific function or class from the assembly (you can for example look for a function with some special name or marked with some attribute).

(As a side-note, using quotations or writing your own interpreter as mentioned in the related SO question may be an option if your source code is relatively simple, but probably not if you want to support the full power of F#)

Community
  • 1
  • 1
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • I am still trying to get my head around quotations and metaprogramming in general. I am pretty sure this is functionality I will not need - but was thinking of being able to provide some test cases via datasets in separate files that I could load in dynamically. CodeDom (ugh - remember plucking away at that pig in 2004ish in c#) will defintitely do the trick. Thank you for your response. – akaphenom Apr 09 '10 at 16:39
  • 2
    **CodeDOM** isn't very elegant technology, but it should do the trick when you just need to invoke the F# compiler (generating F# code using CodeDOM is also possible in theory, but it has some limitations). **Quotations** in F# are mainly used for executing code written in F# in some different execution environment (as SQL, as JavaScript on the client-side, as GPU code), so they're not really useful when you have some program as a string. – Tomas Petricek Apr 09 '10 at 16:57