This describes how phantom types can be used to enforce method preconditions at compile time.
The above code bahave as promised, rejecting at compile time any attempt to open a door that is already open.
The semantics are rather strange, since the Door is actually stateless. Only the compiler knows whether the door is open or closed!
Note how the open and closed Door are the exact same instance.
The type of the values is rather strange.
The repl reports the expected type for dopn
and dopn.close
functions as expected.
This mechanism appears to be overly clever, with limited real world applicability
Consider a more complex domain model, with a Room that contains a single door.
Note how the details of the Door
implementation appear within Room
.
In fact, there is no reason to even reference the actual Door
class.
Now consider scaling this model to cover multiple Doors per Room, Rooms in a Wing of a Building in a Campus.