Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Recursion between class and value?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: tim@f...
Subject: [Caml-list] Recursion between class and value?
I'd like to be able to have a class and a function be mutually
recursive, kind of like this:

   class foo = object
     method bar x =
       if x > 0 then baz (x - 1)
       else ()
   end

   let baz z =
     if z > 0 then
       (new foo)#bar (z - 1)
     else ()

The best I know how to do is transform baz into a method on foo,
yielding this:

   class foo = object (self)
     method bar x =
       if x > 0 then self#baz (x - 1)
       else ()
     method baz z =
       (* Except for the type recursion, this doesn't have to be a 
          method on foo. *)
       if z > 0 then
         (new foo)#bar (z - 1)
       else ()
   end

   let baz z = (new foo)#baz z

The comment points at the ugly part.  Is there a better way?
-- 
Tim Freeman       
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners