Generating F# code

谁说胖子不能爱 提交于 2020-01-01 03:11:17

问题


T4 is the "official" code generation engine for C#/VB.NET. But F# doesn't support it (this is from April, but I couldn't find any newer mentions). So what is a good way to generate F# code?

EDIT:

I want to implement 2-3 finger trees in F#. I already have implemented them in C#, so this should be a nice comparison. 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. And a T4-like approach looks perfect for it...


回答1:


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. Here is how you can do it with 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();
    }
}

#>



回答2:


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

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:

  • http://www.quanttec.com/fparsec/
  • http://www.codeplex.com/fscheck/

[1] http://en.wikipedia.org/wiki/Combinator_library




回答3:


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.

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.




回答4:


I mostly agree with Robert (though there are certainly some situations where using T4 from F# could be very useful). Anyway, maybe it would be interesting to know why do you want to generate F# code? Then we could maybe suggest some typical functional solution to the problem :-).



来源:https://stackoverflow.com/questions/2182618/generating-f-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!