Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
state pattern...
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] state pattern...
From: Michael Wohlwend <>

> thanks for your reply, it's increasing my understanding of the object system, 
> although there are some (or more... :-) open points:
> In:
> class ['state] context s =
>  object (this:'self)
>  ...
> end
> what are the reasons to give 'this' a type, and isn't 'self a
> type-variable? I thought it ('this') has the type of the class (=
> _context) ?

You're correct. In this case, 'self will be bound to the type of the
class. This is useful if you want to write a type using it. But in the
code for context, this is not used, so ":'self" could be omitted.

> I don't understand why and when it is needed to give the
> object a type.
> The same with:
> class state1 = object (this : _ #state) ...  end
> I thought class state1 is a subtype (or implementing the class-type) #state, 
> since the methods are the same. Why does it help to give the object
> a type?

Again you're correct. But here the type annotation forces all methods
of state1 to have the same type as in state. In practice, this avoids
annotating c in the method show. This works with polymorphic methods


Jacques Garrigue