Class Hooks

The Binding Generator C->Haskell : Implementation of Haskell Binding Modules : Class Hooks
Previous: Pointer Hooks
Next: CPP Directives and Inline C Code

## 2.11. Class Hooks

> `{#class [hsid1 =>] hsid2 hsid3#}`

Class hooks facilitate the definition of a single inheritance class hierachy for external pointers including up and down cast functionality. This is meant to be used in cases where the objects referred to by the external pointers are order in such a hierachy in the external API - such structures are encountered in C libraries that provide an object-oriented interface. Each class hook rewrites to a class declaration and one or more instance declarations.

All classes in a hierarchy, except the root, will have a superclass identified by hsid1. The new class is given by hsid2 and the corresponding external pointer is identified by hsid3. Both the superclass and the pointer type must already have been defined by binding hooks that precede the class hook.

The pointers in a hierachy must either all be foreign pointers or all be normal pointers. Stable pointers are not allowed. Both pointer defined as `newtype`s and those defined by type synonyms may be used in class declarations and they may be mixed. In the case of synonyms, Haskell's usual restrictions regarding overlapping instance declarations apply.

The newly defined class has two members whose names are derived from the type name hsid3. The name of first member is derived from hsid3 by converting the first character to lower case. This function casts from any superclass to the current class. The name of the second member is derived by prefixing hsid3 with the `from`. It casts from the current class to any superclass. A class hook generates an instance for the pointer in the newly defined class as well as in all its superclasses.

As an example, consider

> `

>     {#pointer *GtkObject newtype#}
>     {#class GtkObjectClass GtkObject#}

>     {#pointer *GtkWidget newtype#}
>     {#class GtkObjectClass => GtkWidgetClass GtkWidget#}

> `

The second class hook generates an instance for `GtkWidget` for both the `GtkWidgetClass` as well as for the `GtkObjectClass`.

The Binding Generator C->Haskell : Implementation of Haskell Binding Modules : Class Hooks
Previous: Pointer Hooks
Next: CPP Directives and Inline C Code