[英]Generating F# code
T4 is the "official" code generation engine for C#/VB.NET. T4是C#/ VB.NET的“官方”代码生成引擎。 But F# doesn't support it (this is from April, but I couldn't find any newer mentions).
但F#不支持它 (这是从四月开始,但我找不到任何新的提及)。 So what is a good way to generate F# code?
那么生成F#代码的好方法是什么?
EDIT: 编辑:
I want to implement 2-3 finger trees in F#. 我想在F#中实现2-3个手指树 。 I already have implemented them in C#, so this should be a nice comparison.
我已经在C#中实现了它们,所以这应该是一个很好的比较。 The "digits" and nodes of the tree can be represented as arrays, so
树的“数字”和节点可以表示为数组,因此
type 't FingerTree = Empty | Single of 't | Deep of 't array * (('t FingerTree) array) lazy * 't array
However, the maximum size of these arrays is very small, so it'd be nice to have 但是,这些数组的最大大小非常小,所以很高兴
type 't Digit = Digit1 of 't | Digit2 of 't*'t | Digit3 of 't*'t*'t | Digit4 of 't*'t*'t*'t
type 't Node = Node2 of 't FingerTree * 't FingerTree | Node3 of 't FingerTree * 't FingerTree * 't FingerTree
type 't FingerTree = Empty | Single of 't | Deep of 't Digit * ('t Node) lazy * 't Digit
to avoid bounds checking, etc. 避免边界检查等
But then writing all functions on Digit and Node by hand becomes more difficult, and it's better to generate them. 但是手动编写Digit和Node上的所有函数变得更加困难,并且生成它们会更好。 And a T4-like approach looks perfect for it...
而类似T4的方法看起来很完美......
Because F# doesn't support custom tools in solution explorer, you can place your T4 files in a C# or Visual Basic project and redirect their output to your F# project. 由于F#不支持解决方案资源管理器中的自定义工具,因此可以将T4文件放在C#或Visual Basic项目中,并将其输出重定向到F#项目。 Here is how you can do it with T4 Toolbox :
以下是使用T4 Toolbox的方法 :
<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
FSharpTemplate template = new FSharpTemplate();
template.Output.Project = @"..\Library1\Library1.fsproj";
template.Output.File = "Module2.fs";
template.Render();
#>
<#+
class FSharpTemplate: Template
{
public override string TransformText()
{
#>
// Learn more about F# at http://fsharp.net
module Module2
<#+
return this.GenerationEnvironment.ToString();
}
}
#>
It depends what your trying to do. 这取决于你想要做什么。 While it's an approach that's not really suitable for generating templates in the way many T4 examples show, in general I would recommend designing a "combinators library" [1] for code generation or language oriented programming tasks in F#.
虽然这种方法并不像许多T4示例所示的那样真正适合生成模板,但总的来说,我建议设计一个“组合器库”[1]用于F#中的代码生成或面向语言的编程任务。 The idea is to design some combinators to represent the code you're try to generate, generating F# source text from combinators, then compiling this via the code DOM.
我们的想法是设计一些组合器来表示你试图生成的代码,从组合器生成F#源文本,然后通过代码DOM编译它。
However often it would be easier simply to write an interpreter for your combinators rather than generating code. 然而,通常只是为组合器编写解释器而不是生成代码会更容易。
Good examples of combinators in F# are: F#中组合器的好例子是:
[1] http://en.wikipedia.org/wiki/Combinator_library [1] http://en.wikipedia.org/wiki/Combinator_library
I looked around at various options, and ended up for my relatively simple and static code-generation needs using a *.fsx script that uses a TextWriter with fprintf
to write out the generated F# code. 我查看了各种选项,最后使用* .fsx脚本来完成我相对简单和静态的代码生成需求,该脚本使用带有
fprintf
的TextWriter来写出生成的F#代码。
I actually do use FParsec for some parsing work, but as I'm not translating from some other syntax into F#, the two pieces have little to do with each other. 我实际上使用FParsec进行一些解析工作,但由于我没有将其他语法转换为F#,因此这两部分之间几乎没有关系。
I mostly agree with Robert (though there are certainly some situations where using T4 from F# could be very useful). 我主要同意罗伯特(虽然有些情况下使用F#中的T4非常有用)。 Anyway, maybe it would be interesting to know why do you want to generate F# code?
无论如何,也许知道为什么要生成F#代码会很有趣? Then we could maybe suggest some typical functional solution to the problem :-).
然后我们可以建议一些典型的功能解决方案:-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.