Skip to content

F# support #36

@JordanMarr

Description

@JordanMarr

After playing around with the C# implementation, I was thinking about what an F# implementation might look like using Myriad:

C#

[GenerateSerialize]
public partial class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public partial class Person : Serde.ISerialize
{
    void Serde.ISerialize.Serialize(ISerializer serializer)
    {
        var type = serializer.SerializeType("Person", 2);
        type.SerializeField("Name", new StringWrap(Name));
        type.SerializeField("Age", new Int32Wrap(Age));
        type.End();
    }
}

F#

[<GenerateSerialize>]
type Person = 
    {
        Name: string
        Age : int
    }

Generated:

module Person

let toSerializable (person: Person) =
    { new Serde.ISerialize with
        member this.Serialize(serializer: ISerializer) = 
            let type = serializer.SerializeType("Person", 2)
            type.SerializeField("Name", StringWrap(person.Name))
            type.SerializeField("Age", Int32Wrap(person.Age))
            type.End() 
    }

Usage:

{ Name = "John"; Age = 100 }
|> Person.toSerializable
|> JsonSerializer.Serialize
|> printfn "%s"

It wouldn't be quite as user friendly as the C# generator version because:

  • User would be required to manually include generated output file in the .fsproj file
  • Possible .toml config file needed
  • Myriad NuGet package required

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions