Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Recursive classes and subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jerome Vouillon <jerome.vouillon@i...>
Subject: Re: [Caml-list] Recursive classes and subtyping
Hello,


> Why is it that these classes definitions are not accepted by
> the compiler :
> 
> class a =
[...]
> and b =
> object (this)
[...]
>   initializer this#set_al [(c :> a)] 
> end
> and c =
[...]
>
> while these ones can be compiled:
> 
> class a =
[...]
> class c =
[...]
> class b =
> object (this)
[...]
>   initializer this#set_al [(c :> a)] 
> end
> 
> Where is the fundamental difference ????

In the first case, the type "a" is not fully known when the coercion
"(c :> a)" is performed, as the classes are mutually recursive.  The
compiler handles this case as if it knew nothing about the structure
of the type "a".  With this assumption, the only case the compiler can
be sure that the type of "c" is a subtype of "a" is when the type of
"c" is "a".  This is not the case, hence the error message.

The reason for this overly conservative (in this case) assumption is
that the result of the type inference algorithm could otherwise depend
on the order in which the program is typed.

If you really need mutually recursive classes, a work-around is to
explicitely define the classe types before defining the classes.

-- Jérôme
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr