On 2011/09/08, at 6:29, Dmitry Grebeniuk wrote:
> Hello.
>
> I have an usual question. I want to make some object
> types (container-like) that can map the stored contents
> with its method. For example, let the container will be
> the simple list:
> class ['a] (lst : list 'a) = object ...
> method map : ('a -> 'b) -> 'b lst
>
> The compiler yields an error:
> # class ['a] lst x = object
> method map : 'b . ('a -> 'b) -> 'b lst
> = fun f -> List.map f x end;;
> Error: This type scheme cannot quantify 'b :
> it escapes this scope.
The answer is unfortunately short: there is no solution.
Namely recursive types in ocaml must be regular.
I.e., they must expand to a finite graph.
In particular this means that, inside the class lst, all occurences
of lst must have exactly the parameter 'a.
As you have already found, you can avoid this problem by
defining a record which "hides" the use of lst.
Namely, you must break the cycle with a datatype,
either record or sum type.
Unfortunately this also means that this type has to belong
to some module, and you also lose subtyping.
Jacques Garrigue
--
Caml-list mailing list. Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs