打印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屋!