Search code examples
f#websharper

WebSharper Interface Generator - Records


How do I go about defining a record that would be serialized to a JSON object... I've been trying to build the oConfig parameter for YUI2 constructor, something like:

type TreeParameter =
    {
        Type : string
        Label : string
        Expanded : bool
        Children : TreeParameter array
    }

Thanks!

David


Solution

  • I guess we could implement this but it has not yet made it into the interface generator. For now you can do:

    let TreeParameter =
        let self = Type.New()
        Pattern.Config "TreeParameter" {
            Required =
                [
                    "Type", T<string>
                    "Label", T<string>
                    "Expanded", T<bool>
                    "Children", Type.ArrayOf self
                ]
            Optional = []
        }
        |=> self
    

    From F# point of view the generated type will look like this:

    type TreeParameter(t: string, l: string, e: bool, c: TreeParameter[]) =
        member this.Type = t
        member this.Label = l
        member this.Expanded = e
        member this.Children = c
    

    From JavaScript perspective, the values would look like this:

    {Type:t,Label:l,Expanded:e,Children:c}
    

    In essence it is like a record without the benefit of record syntax and functional extension.