Banner


Algebraic Data Types




Sven Eric Panitz


Java Annotations for Algebraic Data Types

Algebraic data types are known from functional programming languages. An algebraic type is defined by a set of constructors. Usually algebraic types are recursive and represent some hierarchical structure. Algorithms on algebraic types traverse over the tree structure. Typical examples for algebraic types are defined by XML schemas or DTDs.

In functional language tree traversal can be easily implemented through pattern matching on the different constructors. In object oriented languages the visitor pattern is the standard means for implementing pattern matching. Implementing algebraic types and visitors in Java amounts in a lot of boilerplate code.

The paper shows, how Javas new annotation mechanism can be used to define algebraic types in an elegant way similar to the definitions in functional languages. The complete type gets generated through a generator class, which is invoked through Java's annotation processing tool apt.

The paper contains the complete sources of the generator together with some examples.

Resources