Use structure format display attribute

Category:
Format
Description: Use structure format display attribute. Use this attribute we can customerize the printf content.
Code:
[<StructuredFormatDisplayAttribute("MyType is {Contents}")>]
type C(elems:int list) =
member x.Contents = elems

let pritnfnSample0() =
printfn "%A" (C [1..4])

Execution Result:
MyType is [1; 2; 3; 4]

Use structure format display complex structure

Category:
Format
Description: Use structure format display complex structure
Code:
[<StructuredFormatDisplayAttribute("MyType is {Contents}")>]
type Tree(node: int, elems: Tree list) =
member x.Contents = (node, elems)

let printfnSample1() =
let c = Tree (1, [ Tree (2, []); Tree (3, [ Tree (4, []) ]) ])
let c2 = Tree (1, [ c; c])
let c3 = Tree (1, [ c2; c2])
printfn "%A" c3

Execution Result:
Tree (1,
[Tree (1,
[Tree (1, [Tree (2, []); Tree (3, [Tree (4, [])])]);
Tree (1, [Tree (2, []); Tree (3, [Tree (4, [])])])]);
Tree (1,
[Tree (1, [Tree (2, []); Tree (3, [Tree (4, [])])]);
Tree (1, [Tree (2, []); Tree (3, [Tree (4, [])])])])])

Last edited Sep 12, 2011 at 11:33 PM by ttliu2000, version 2

Comments

No comments yet.