Printing F# discriminated union

前端 未结 3 732
迷失自我
迷失自我 2021-01-04 22:43

I am writing a F# program which parses a string into a AST type which is a discriminated union.

When I use fsi (on Mono + Mac OS X) to run my code, the

相关标签:
3条回答
  • 2021-01-04 23:23

    In addition to Daniel's comment, here is a good blog article explaining how to format it in whatever way you'd wish:

    http://blogs.msdn.com/b/dsyme/archive/2010/01/08/some-tips-and-tricks-for-formatting-data-in-f-interactive-and-a-in-sprintf-printf-fprintf.aspx (web.archive.org)

    0 讨论(0)
  • 2021-01-04 23:25

    To convert a discriminated union into a string, you should use sprintf "%A" ast instead of ast.ToString().

    If you want Enum.GetName, you can use Microsoft.FSharp.Reflection namespace. See What is the Enum.GetName equivalent for F# union member?.

    0 讨论(0)
  • 2021-01-04 23:43

    Have you tried printfn "%A" ast? The %A specifier takes into consideration the StructuredFormatDisplayAttribute[MSDN], if present.

    0 讨论(0)
提交回复
热议问题