5

I am trying to use F# as a REPL and scripting which uses 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 am writing a script with the same expression and want it to print same output. But I cannot find any print function which would do it. The closest I could find is printfn "%O" which uses ToString() method, which is not defined in my case and just prints the object type.

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

How to generate the F# type signature similar to FSI in my own code? seems to be focused on type, and I basically need rather pretty-printed value.

PS: seems like it is code which is internal to fsi.exe. See fsi.fs and sformat.fs sources. I tried to invoke them through reflection, but simple Internal.Utilities.StructuredFormat.any_to_string(value) printed just a type. Would still be nice if anybody knows how to invoke it correctly, but for time being I decided not to spend more efforts on it.

max630
  • 8,762
  • 3
  • 30
  • 55
  • 2
    Possible duplicate of [How to generate the F# type signature similar to FSI in my own code?](http://stackoverflow.com/questions/9908052/how-to-generate-the-f-type-signature-similar-to-fsi-in-my-own-code) – TheInnerLight Nov 17 '16 at 12:50
  • what about: http://stackoverflow.com/questions/791706/how-do-i-customize-output-of-a-custom-type-using-printf? – Helge Rene Urholm Nov 17 '16 at 13:13
  • @helge-rene-urholm that seems to involve changing the object type itself; I cannot do it because it's from library. Actually, I wonder how they achieved "both print statements yield: {a = 5;}" result there; this is exactly what I'd need – max630 Nov 17 '16 at 13:20
  • 1
    Have you tried `printfn "%A"`? It sounds like that's what you're actually looking for, not `%O`. – rmunn Nov 17 '16 at 13:25
  • @rmunn yes I did, both just prints the object's class name – max630 Nov 17 '16 at 13:30

1 Answers1

0

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

For a list:

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

For a 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 non-F# datatype:

> let u = UriBuilder("http", "bar", 80);;
val u : UriBuilder = http://bar:80/
> printfn "%A" u;;
http://bar:80/
Anton Schwaighofer
  • 3,119
  • 11
  • 24
  • it looks like properly defined .ToString method. In my case there was none. The output from REPL prints object internals then. – max630 Feb 23 '17 at 17:37
  • I think then I still don't get your issue. What are the datatypes for which you get a different output in REPL, when compared to what you get when using `printfn "%A"` ? Some C# class? – Anton Schwaighofer Feb 23 '17 at 20:31
  • Yes, C# class. From some library which is not mine. I'll try to provide example as I have time, but as far as I remember it was very straightforward. – max630 Feb 24 '17 at 08:14