Skip to content

varlink: tagged union type #234

Description

@swick

Varlink currently (v1) supports enums and structs, but not tagged unions/tagged enums. Tagged unions are a type where the type of the content is defined by the enum variant (the tag).

This is especially useful when doing a more call, and there are state changes between replies, and the content of the message changes based on that state. The current workaround for the lack of this type is usually a struct with lots of nullable fields, and then code that manually pokes at those fields based on an enum.

type TaggedUnionEmulation (
   tag: (one, two),
   content_one: ?(foo: bar),
   content_two: ?(bar: baz),
)

A new tagged union type would allow to define this type natively. It avoids the nullable workaround, and allows a varlink implementation to properly type check, instead of having each handler write specific code for it. Better suggestions for the syntax are very welcome.

type TaggedUnion union(
   one: (foo: bar),
   two: (bar: baz),
)

This could be translated to the following JSON:

{
  "tag": "org.example.TaggedUnion.one",
  "content": {"foo": ... }
}

This already exists in varlink as the error type, where the tag is "error", and the content is "parameters". It is however part of the protocol layer, and is also embedded in the parent structure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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