打印 F# 可区分联合 [英] Printing F# discriminated union
问题描述
我正在编写一个 F# 程序,该程序将一个字符串解析为一个 AST 类型,这是一个可区分的联合.
I am writing a F# program which parses a string into a AST type which is a discriminated union.
当我使用 fsi
(在 Mono + Mac OS X 上)运行我的代码时,AST 以一种很好的格式打印出来.但是当我使用 printfn "%s" <|ast.ToString()
我得到类似 FSI_0002.Absyn+clazz
的东西.为所有可区分的联合类型编写一个 ToString
方法将是一项繁重的工作.
When I use fsi
(on Mono + Mac OS X) to run my code, the AST is printed out in a nice format. But when I use printfn "%s" <| ast.ToString()
I get something like FSI_0002.Absyn+clazz
. Writing a ToString
method for all the discriminated union types would be a big chore.
如何按照 fsi
的方式打印值?
How do I make the value print the way fsi
does it?
推荐答案
你试过 printfn "%A" ast
吗?%A
说明符考虑了 StructuredFormatDisplayAttribute
[MSDN],如果存在.
Have you tried printfn "%A" ast
? The %A
specifier takes into consideration the StructuredFormatDisplayAttribute
[MSDN], if present.
这篇关于打印 F# 可区分联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!