Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing F# discriminated union

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?

like image 812
Binil Thomas Avatar asked Sep 17 '11 02:09

Binil Thomas


People also ask

What does print f do?

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.

What is print f ') in Python?

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 {} .

What does F mean in printing?

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.

How do you print f string?

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.


1 Answers

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

like image 73
Daniel Avatar answered Nov 03 '22 00:11

Daniel