How to print from F # e.g. REPL

I am trying to use F # as REPL and scripts that use the C # library. When I evaluate an object in REPL, it prints its internal structure:

> <expression>;; val it: <type> = <subtype> {<prop> = <value>; ... <prop> = <value>;} 

Then I write a script with the same expression and want it to print the same output. But I can not find the print function that would do this. The closest I could find is printfn "%O" , which uses the ToString () method, which is not defined in my case and just prints the type of the object.

This seems to be a simple question, but I cannot find it here or anywhere on Google.

How to generate a signature like F #, similar to FSI in my own code? seems to be type oriented, and I basically need some pretty printed value.

PS: it looks like this is the code that is internal to fsi.exe. See fsi.fs and sformat.fs sources. I tried to call them through reflection, but simple Internal.Utilities.StructuredFormat.any_to_string(value) printed only the type. It would be nice if someone knew how to properly call him, but for a while I decided not to spend more effort on him.

+5
source share
1 answer

I had the same problem, but in my case printfn "%A" gave exactly the same result as what I see in F # Interactive (bar indent):

For the list:

 > let l = [(2,"a")];; val l : (int * string) list = [(2, "a")] > printfn "%A" l;; [(2, "a")] 

For the record:

 > type R = { A: string; B: int };; type R = {A: string; B: int;} > let r = { A = "Foo"; B = 1 };; val r : R = {A = "Foo"; B = 1;} > printfn "%A" r;; {A = "Foo"; B = 1;} 

For a data type other than F #:

 > let u = UriBuilder("http", "bar", 80);; val u : UriBuilder = http://bar:80/ > printfn "%A" u;; http://bar:80/ 
0
source

Source: https://habr.com/ru/post/1259904/


All Articles