[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-09-14 (12:02) |
From: | Jacques GARRIGUE <garrigue@k...> |
Subject: | Re: [Caml-list] object question |
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