caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
To: micha-1@fantasymail.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] object question
Date: Wed, 14 Sep 2005 21:01:56 +0900 (JST)	[thread overview]
Message-ID: <20050914.210156.68535209.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <200509141000.50364.micha-1@fantasymail.de>

From: Michael Wohlwend <micha-1@fantasymail.de>

> I want to make some chained object and an "apply" function on this chain.
> it might look as this (the functions don't make sense...yet :)
> 
> -----------------------------------------------
> class type tst = object
>     method next: tst
>     method clr: unit
>     method apply: (tst -> unit) -> unit
> end;;
> 
> let empty : tst = object(self)
>     method next = self 
>     method clr = ()
>     method apply fkt = ()
> end;;
> 
> class test : tst = object(self)
>     val mutable n = empty
>     method clr = ()
>     method next = n
>     method apply fkt = fkt (self :> tst) ; self#next#apply fkt   (* Line 17 *)
> end;;
> 
> let a = new test in
> a#apply (fun o -> o#clr)
> ----------------------------------------------------
> 
> first question, why do I have to cast self to tst in Line 17? The class test 
> is defined to be of type tst, so self should allready be of type tst. But it 
> works only with the cast.

self is not of type tst, but of the type of the current subclass (as
test could be extended later). On the other hand apply expects a
function of type tst -> unit, so you need a cast. You could avoid it
by having
  object ('a)
   ...
   apply : ('a -> unit) -> unit
  end

> The other question, does this "apply" function use up the stack? Would it be 
> better to define a tail-recursive apply outside the class?

Since this is a tail-call, this should be ok.

Jacques Garrigue


      reply	other threads:[~2005-09-14 12:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-14  8:00 Michael Wohlwend
2005-09-14 12: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=20050914.210156.68535209.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=micha-1@fantasymail.de \
    /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).