asdlGen Reference Manual : Views : Choosing a Different Representation
Previous: Other Properties
Next: Examples

5.4. Choosing a Different Representation

module IntMap {
  int_map = (int size,entries map)
  entries = (entry* entries)
  entry   = (int key, int value)
}
The above is one possible abstract description of a mapping from integers to integers. It would be more efficient to implement such a mapping as a binary tree. Described as with the ASDL definition below.
module IntMap {
  int_map = (size int,map tree)
     tree = Node(int key,int value,tree left,tree right)
          | Empty
}

Although this is a much more efficient representation it exposes implementation details. If we decided to change the implementation of int_maps to use a hash table the all other clients that use our type will have to be updated.

The view properties natural_type, natural_type_con, wrapper, and unwrapper provide a general mechanism to choose a different more efficient representation through coercion functions. All of these properties apply to types only and are interpreted as qualified identifiers.

natural_type

The type to use in place of the original type in all the resulting code. Supported by all output languages.

natural_type_con

A unary type constructor to apply to the old type to get a new type to use in all the resulting code. e.g. ref in ML to make a type mutable. Supported by ML and Haskell. Support for C++ templates will be added in the near future.

wrapper

A function to convert the new type to the old type when writing the pickle. Supported by all output languages.

unwrapper

A function to convert the old type to the new type when reading the pickle. Supported by all output languages.

When using natural_type and natural_type_con the automatically generated type definitions for the original type still remain, but all other references to the original type in constructors, picklers, and other type definitions that referred to it are replaced with the new type. The original definition must remain to support pickling of the type. Pickling is achieved by appropriately coercing the new type to the old type and vice versa with functions specified by wrapper and unwrapper properties.

5.4.1. Examples


asdlGen Reference Manual : Views : Choosing a Different Representation
Previous: Other Properties
Next: Examples