14 Abstract Types

Recall that when a signature doesn't specify a value, the value cannot be used outside a sealed module of that type. This applies to types as well. Let's use our example of ListStack from before.

We can see that ListStack.stack has a type of 'a list; however, if the module is sealed, we can't use list values with it:

Note how the second declaration failed. This is because type ListStack.stack isn't necessarily a list. We don't get to know or use the type of it. These two pieces of code would create the same structure, but the fact that the stack is a list is hidden behind the signature.

This is a good thing, because it lets us use the same signature for different implementations. You can think of a sealed class as a "private version," and an unsealed class a "public version".

Leaving the implementation off of a type is known a an abstract type. Abstract types give us encapsulation. Clients don't need to know how it works. Clients tend to exploit this, for example, say you want to upgrade to a two-list queue, you'd have broken code. This means that encapsulation is a good thing!

A common idiom for abstract types is to name the type t, for example:

OCaml programmers tend to expect t to be the main implementation.