DBUS_TYPE_SUM ? DBUS_TYPE_UNIT ? DBUS_TYPE_DEPENDENT_PAIR ?

Andrew Cann shum at canndrew.org
Sat Dec 14 03:38:24 PST 2013


On the subject of new types that could be added to DBus, it would be cool if 
there was a type for representing sum types in languages with algebraic
datatypes. For instance, the DBus equivalent of the haskell type

data MyType = Foo Int32
            | Bar Int32
            | Baz String

would have the signature "[iis]", meaning a value of this type would be either
the first int32, the second int32, or a string. The DBus specification already
mentions a Maybe type being planned for a future extension, ie. a type where
a value with the signature "mi" could be either an int32 or nothing. A sum type
could be used as a generalisation of this so "mi" would instead be "[()i]", ie.
either the empty struct or an int32. Better yet, introduce a unit/void type
with the signature ".", then a Maybe Int32 could be signed "[.i]".

Another, perhaps crazier, but potentially useful type is the dependent pair
type where a value can depend on a type passed at runtime. This could be useful
for sending generic types over dbus.

I've been in a situation before where I wanted to pass an array of variants
down the wire but I wanted all the array elements to be of the same, unknown,
type. You could handle sitations like this by having a type that consists of a
signature for a type T, and a value of a type defined in terms of T. With a
type like this you could express the aforementioned array type as "pa+", where
"p" stands for pair and "+" refers to the type defined by the enclosing pair.
ie. "pa+" would be interpreted as: 'A pair of a signature and an (array of
(the type defined by the enclosing pair))'. Using this type, the variant type
"v" would be equivalent to "p+" and the signature type "g" would be equivalent
to "p()" or "p.". If you wanted to express types dependent on more than one
unknown type you would have to introduce another signature symbol, say "-",
that escapes one level of pairing, ie. "+" refers to the type defined by the
innermost "p", "-+" refers to the type defined by the next outer "p", "--+" for
the next one etc. Using this, you could specify very complicated types. For
example the type 'Arrays of structs of two values of the same type' would be
signed "ap(++)" and 'Dictionaries of unknown key and values types' would be
signed "ppa{-++}".

Anyway, just some ideas. By the way are there actually any real plans to extend
DBus with major new features like this? Or at this point is the specification
pretty much set in concrete?

 - Andrew



More information about the dbus mailing list