Skip to content

Support single-case discriminated union #71

Description

@drhumlen

It would be nice if single-case discriminated unions was serialized directly:

type PersonId = PersonId of int
type Person = 
  { Id: PersonId
    Name: string }
{"id": 123, "name": "Foo"}

instead of

{"id": {"PersonId": 123}, "name": "Foo"}

Edit: I realize there's some cases where this will do something very different from what's intended; e.g. if you have

type AuthType =
   | ApiToken of string

With the intention of adding more cases in the future, and then you'll break the serializer when you add the next case.

So I'm propising that we only do the single-case DU "optimization" when: There's one and only one case of course, and that case only has 1 field(?), and one or both of the following:

  1. The name of the case is the same as the type. So type UserId = UserId of string. If it's type UserId = PersonId of string, then that's a no-match. (?)
  2. When the argument is a "primitive" – string, number, long, etc. I can't immediatly think of a case where you want a single-case DU for something else, but I might be wrong about that (?).

I think I like 1 more, even though it's slightly more "magical", but I think it's most pragmatic way of doing it. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions