Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: circular types?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Rogoff <bpr@b...>
Subject: Re: circular types?
On Thu, 26 Oct 2000, John Max Skaller wrote:
> Brian Rogoff wrote:
> > And if you can do that then allowing mutual recursion between
> > classes and types also should be allowed.
> 
> I think this can be done now. Here is the method: to have a type t
> and a class c mutually dependent, for example:
> 
> 	class type c = object x : t end
> 	and type  t = C c | Null
> 
> write
> 
> 	class type c' ['t] = object x : 't end
> 	type t = C t c' | Null
> 	class type c = t c'
> 
> and you have it. In other words, abstract out the types from
> the classes by making them type parameters, declare the types,
> using appropriately instantiated class types, and then name the
> required class instances.

Yes, it's the same trick that I pointed out for module/type recursions; as 
they say any problem can be solved by another level of indirection. I'd
like to be able to express this directly with an "and" of course.

Check your syntax too, it's really

class type ['a] c' = object method x : 'a end;;
type t = C of t c' | Null;;
class type c = object inherit [t] c' end;;

and note that this works with virtual classes as well as class types 

class virtual ['a] c' = object method virtual x : 'a end;;
type t = C of t c' | Null;;
class virtual c = object inherit [t] c' end;;
class foo a_t = object inherit c method x = a_t end;;
let f0 = new foo Null;;
let f1 = new foo (C f0);;

Before anyone complains about those horrid double semi-colons I point out
that those lines are all accepted by the top-level ;;-)

-- Brian