Generating F# code

前端 未结 4 1639
春和景丽
春和景丽 2021-02-06 01:58

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 g

4条回答
  •  日久生厌
    2021-02-06 02:24

    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

提交回复
热议问题