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
[Caml-list] sum types with objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-11-26 (09:33)
From: Tom Hirschowitz <Tom.Hirschowitz@e...>
Subject: [Caml-list] sum types with objects

Hi all,

when trying to implement lists with objects and classes, I run into
the following issue.

The idea is to define a class type list, parameterized over the type
'a of elements, and containing the methods is_nil, hd, tl, map. Then,
I planned to define two classes nil and cons for building objects of
type list.

The problem arises during the typing of list:

# class type ['a] list = object
  method is_nil : bool
    method hd : 'a
    method tl : 'a list
    method map : ('a -> 'b) -> 'b list
class type ['a] list =
    method hd : 'a
    method is_nil : bool
    method map : ('a -> 'a) -> 'a list
    method tl : 'a list

The method map has type ('a -> 'a) -> 'a list, which is not what I
want.  Trying to explicitely quantify 'b is also wrong, because 'b
escapes its scope.

Does someone know how to make this work?

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: