Can I use multiple properties in the StructuredFormatDisplayAttribute attribute?

I play with StructuredFormatDisplay, and I suggested that I can use several properties for Value, but it seems that it is not. This question (and the accepted answer) talks about tuning in general, but in the examples given, only one property is used. MSDN does not help when it comes to using this attribute.

Here is my example:

[<StructuredFormatDisplay("My name is {First} {Last}")>]
type Person = {First:string; Last:string}

If I then try this:

let johnDoe = {First="John"; Last="Doe"}

I get this error:

<StructuredFormatDisplay exception: Method 'FSI_0038+Person.First} {Last' not found.>

The error seems to hint that it only captures the first property mentioned in mine Value, but it's hard for me to say this with certainty.

I realized that I could get around this by declaring my type as follows:

[<StructuredFormatDisplay("My name is {Combined}")>]
type Person = {First:string; Last:string} with
    member this.Combined = this.First + " " + this.Last

, - , , , .

- source :

F # PreText {PropertyName} PostText

, , , , -, , , , .

+4
2

F # sformat.fs, 868. , :

let p1 = txt.IndexOf ("{", StringComparison.Ordinal) 
  let p2 = txt.LastIndexOf ("}", StringComparison.Ordinal) 
  if p1 < 0 || p2 < 0 || p1+1 >= p2 then  
      None  
  else 
    let preText = if p1 <= 0 then "" else txt.[0..p1-1] 
    let postText = if p2+1 >= txt.Length then "" else txt.[p2+1..] 
    let prop = txt.[p1+1..p2-1] 
    match catchExn (fun () -> getProperty x prop) with 
    | Choice2Of2 e -> 
        Some (wordL ("<StructuredFormatDisplay exception: " + e.Message + ">")) 
    | Choice1Of2 alternativeObj -> 
        let alternativeObjL =  
          match alternativeObj with  
          | :? string as s -> sepL s 
          | _ -> sameObjL (depthLim-1) Precedence.BracketIfTuple alternativeObj 
        countNodes 0 // 0 means we do not count the preText and postText  
        Some (leftL preText ^^ alternativeObjL ^^ rightL postText) 

, , { }, . , foo {A} {B} bar A} {B.

, , . , F # GitHub !

+4

, PR, , , 4.0.

, F # 4.0, StructuredFormatDisplay, , , , \ (, "I love \{ braces").

. , , , - .

+3

All Articles