F# 相当于 Eval [英] F# equivalent to Eval

查看:25
本文介绍了F# 相当于 Eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有等效于 eval 的 F#?我的意图是让我的应用程序从文件中加载一个小代码示例,并且基本上

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)

我是 F# 的新手,在将其应用于项目之前试图找出一些限制.

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

谢谢

推荐答案

没有允许您直接执行此操作的函数.但是,当您想要以编程方式编译 F# 源文件时,您可以从应用程序中调用 F# 编译器.最简单的方法是使用 F# CodeDOM 提供程序,它作为 F# PowerPack 的一部分提供(在 FSharp.Compiler.CodeDom.dll 程序集中).但是,请注意,您需要在用户的计算机上安装 F# 编译器.

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")

一旦您将源文件编译成程序集,您将需要使用 .NET 反射加载程序集并从程序集中运行某些特定的函数或类(例如,您可以查找具有某些特殊名称或标记的函数)具有某些属性).

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).

(作为旁注,使用引用或编写自己的解释器,如 相关的 SO 问题 如果您的源代码相对简单,则可能是一个选项,但如果您想支持 F# 的全部功能,则可能不是这样)

(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#)

这篇关于F# 相当于 Eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆