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
class constraints
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-27 (14:22)
From: Peng Zang <peng.zang@g...>
Subject: Re: [Caml-list] class constraints
Hash: SHA1

Perhaps you mean this?

  class type foo = object('self)
    method get_parent : 'self

  class type foo2 = object('self)
    constraint foo2 = #foo
    method baz : float
    method get_parent : 'self

The error you get is because the first get_parent returns a foo, while the 
second returns a foo2.  While you can write a function that takes "foo or 
foo2" or really anything that is "foo-able", but you can't write a function 
that returns "foo or foo2".  When it comes to return types you have to choose 
what to return.  At least.. that's how I understand it.


On Friday 27 March 2009 10:06:03 am Jacques Le Normand wrote:
> Dear list,
> I'm having a little problem understanding constraints in objects. Consider
> the following code:
> class type foo =
> object
>   method get_parent : foo
> end
> class type foo2 =
> object
>   constraint foo2 = #foo
>   method baz : float
>   method get_parent : foo2
> end
> it gives me the following error:
> The abbreviation foo2 expands to type < baz : float; get_parent : foo2 >
> but is used with type #foo
> but foo2 is clearly a subtype of foo. What am I doing wrong? I'm using
> ocaml 3.10.2
> --Jacques Le Normand
Version: GnuPG v2.0.7 (GNU/Linux)