recursive types, struct, custom, dict, etc.

Duncan Coutts duncan.coutts at
Tue Jun 8 07:03:19 PDT 2004

Hi all,

>From a language bindings point of view, a type system with fully
recursive types would make marshaling much easer for the user.

For example in Haskell we could create a DbusMarshalable interface and
make all the atomic types implement the interface, then we just make
tuples (records) and lists (of DbusMarshalable things) implement the
interface. Then we've already got a large collection of common types
that the user can send over the wire without any custom marshaling code.
Eg if they had a type they wanted to send like

[(String, [(Bool, Int)])]

That is a list of pairs of strings and lists of pairs of bool and int

or in your notation: a{sa{bi}}

Then this type already implements DbusMarshalable by virtue of it being
composed of types that implement DbusMarshalable. Very convenient.


For the interested I'll sketch an implementation:

-- the interface
class DbusMarshalable a where
  dbusMarshalTypeCode :: a -> String		--eg "a{i}"
  dbusSerialise       :: a -> DbusWireFormat
  dbusUnserialise     :: DbusWireFormat -> a

-- implement the interface for a couple atomic types
instance DbusMarshalable Int where
  dbusMarshalTypeCode _ = "a"
  dbusSerialise = ...
  dbusUnserialise = ...
instance DbusMarshalable String where
  dbusMarshalTypeCode _ = "s"
  dbusSerialise = ...
  dbusUnserialise = ...

--now lists and records
instance DbusMarshalable a => DbusMarshalable [a] where
  dbusMarshalTypeCode _ = "a{" ++ (dbusMarshalTypeCode (undefined::a)) ++ "}"
  dbusSerialise = ...
  dbusUnserialise = ...

-- tuple of size two
instance (DbusMarshalable a, DbusMarshalable b) => DbusMarshalable (a,b) where
  dbusMarshalTypeCode _ = dbusMarshalTypeCode (undefined::a)
                       ++ dbusMarshalTypeCode (undefined::b)
  dbusSerialise = ...
  dbusUnserialise = ...

-- using some generic programming extension you could define instances for all
-- user-defined records, not just built-in tuples of fixed sizes.

More information about the dbus mailing list