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.
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.
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.
This could be translated to the following JSON:
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.