From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id EAA27411; Mon, 19 Aug 2002 04:04:57 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA27807 for ; Mon, 19 Aug 2002 04:04:56 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g7J222j25504 for ; Mon, 19 Aug 2002 04:02:16 +0200 (MET DST) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id LAA11233; Mon, 19 Aug 2002 11:01:19 +0900 (JST) To: tim@fungible.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Recursion between class and value? In-Reply-To: <20020818174932.712167F63@lobus.fungible.com> References: <20020818174932.712167F63@lobus.fungible.com> X-Mailer: Mew version 1.94.2 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020819110119P.garrigue@kurims.kyoto-u.ac.jp> Date: Mon, 19 Aug 2002 11:01:19 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: tim@fungible.com (Tim Freeman) > 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 recursion is between baz and the constructor of foo (not the type). So this is only a value level problem. While your solution is ok: > The best I know how to do is transform baz into a method on foo, > yielding this: A simpler one is to make new foo a parameter of baz: let baz ~new_foo z = if z > 0 then new_foo#bar (z - 1) class foo = object method bar x = if x > 0 then baz ~new_foo:(new foo) (x - 1) end let baz n = baz ~new_foo:(new foo) n A little heavy, but pretty classical. An even heavier approach, but maybe more intuitive, is to put baz in another class, do that you can access foo's constructor. Jacques Garrigue ------------------- 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