我将C#项目移植到F#,并且有一个代表SQL AST的相当大的类型层次结构。在层次结构的基本类型上,重写SqlNode
,ToString
以调用在SqlNode
的子类型上运行的漂亮打印机。像这样:
type SqlNode =
override x.ToString() = SqlPrinter.Print x
...
// a bunch of node types deriving from SqlNode
...
type SqlPrinter =
static member Print(node: SqlNode) =
match node with
| :? SqlSelect -> ...
| :? SqlInsert -> ...
// etc.
如何在不将格式化逻辑放入节点本身的情况下将其移植到F#?