caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: tim@fungible.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Recursion between class and value?
Date: Mon, 19 Aug 2002 11:01:19 +0900	[thread overview]
Message-ID: <20020819110119P.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20020818174932.712167F63@lobus.fungible.com>

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


      reply	other threads:[~2002-08-19  2:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-18 17:42 Tim Freeman
2002-08-19  2:01 ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020819110119P.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=tim@fungible.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).