1 people like it.

Customization of formatting via ToString and StructuredFormatDisplay

Demonstration of FSI/printer behavior in the presence of ToString and StructuredFormatDisplayAttribute

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
open System

type t(name : string, age : int) = class
  member val name = name
  member val age = age
end

type t_ToString(name : string, age : int) = class
  inherit t(name, age)
  override __.ToString() = sprintf "t_ToString(\"%s\", %d)" name age
end

[<StructuredFormatDisplay("t_SFD (with name=\"{name}\")")>]
type t_SFD(name : string, age : int) = class
  inherit t(name, age)
  override __.ToString() = sprintf "t_SFD(\"%s\", %d)" name age
end

let pr x = printfn "ToString:\n%O\n\n%%A:\n%A\n=====" x x

let t = t("t", 7777)
(* val t : t *)
let tts = t_ToString("tts", 8888)
(* val tts : t_ToString = t_ToString("tts", 8888) *)
let tsfd = t_SFD("tsfd", 9999)
(* val tsfd : t_SFD = t_SFD (with name="tsfd") *)

#if !INTERACTIVE
[<EntryPoint>]
let main _ =
#else
let _ =
#endif
  pr t;
  pr tts;
  pr tsfd;
  0

(*
ToString:
FSI_0002+t

%A:
FSI_0002+t
=====
ToString:
t_ToString("tts", 8888)

%A:
t_ToString("tts", 8888)
=====
ToString:
t_SFD("tsfd", 9999)

%A:
t_SFD (with name="tsfd")
=====
 *)
namespace System
type t =
  new : name:string * age:int -> t
  member age : int
  member name : string

Full name: Script.t
val name : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val age : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
type t_ToString =
  inherit t
  new : name:string * age:int -> t_ToString
  override ToString : unit -> string

Full name: Script.t_ToString

--------------------
new : name:string * age:int -> t_ToString
Multiple items
new : name:string * age:int -> t

--------------------
type t =
  new : name:string * age:int -> t
  member age : int
  member name : string

Full name: Script.t
override t_ToString.ToString : unit -> string

Full name: Script.t_ToString.ToString
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
type StructuredFormatDisplayAttribute =
  inherit Attribute
  new : value:string -> StructuredFormatDisplayAttribute
  member Value : string

Full name: Microsoft.FSharp.Core.StructuredFormatDisplayAttribute

--------------------
new : value:string -> StructuredFormatDisplayAttribute
Multiple items
type t_SFD =
  inherit t
  new : name:string * age:int -> t_SFD
  override ToString : unit -> string

Full name: Script.t_SFD

--------------------
new : name:string * age:int -> t_SFD
override t_SFD.ToString : unit -> string

Full name: Script.t_SFD.ToString
val pr : x:'a -> unit

Full name: Script.pr
val x : 'a
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val t : t

Full name: Script.t
new : name:string * age:int -> t
val tts : t_ToString

Full name: Script.tts
val tsfd : t_SFD

Full name: Script.tsfd
Multiple items
type EntryPointAttribute =
  inherit Attribute
  new : unit -> EntryPointAttribute

Full name: Microsoft.FSharp.Core.EntryPointAttribute

--------------------
new : unit -> EntryPointAttribute
Raw view New version

More information

Link:http://fssnip.net/7W5
Posted:6 months ago
Author:Mark Laws
Tags: classes , debugging , formatting , fsi , objects , printf