打印F#区分的联合 [英] Printing F# discriminated union

查看:104
本文介绍了打印F#区分的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆