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 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.
How do I make the value print the way fsi
does it?
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.
The f means Formatted string literals and it's new in Python 3.6 . A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F' . These strings may contain replacement fields, which are expressions delimited by curly braces {} .
The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.
To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark in a print() statement. Inside this string, you can write a Python expression between { } characters that can refer to variables or literal values.
Have you tried printfn "%A" ast
? The %A
specifier takes into consideration the StructuredFormatDisplayAttribute
[MSDN], if present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With