Typeclasses

Typeclasses are a way of grouping classes and types with related functionality. For example, say you wanted to assert that your function should only accept numeric types. One option would be to use the utility class IsOneOf() like so:

IsOneOf(int, float, long, complex)

But what if all you care about is what those types can do, e.g., be added, subtracted, etc? If this is the case, and you were to use IsOneOf, you've limited your opportunity for future expansion.

Typeclasses to the rescue! The Typeclass() utility class is used to infer a common interface from a set of types. Put simply, you feed Typeclass() a bunch of types and/or classes and it will deduce the set of methods common to all these types. The resulting Typeclass instance will approve any object implementing this minimal interface.

We've coded up a bunch of basic Typeclass instances to get you started. All of the following instances live in the typecheck.typeclasses submodule.

Index

  1. ImSequence - immutable sequences
  2. Mapping - the mapping interface
  3. MSequence - mutable sequences
  4. Number - numeric types
  5. String - string types

Valid XHTML 1.0 Transitional