* object question
@ 2005-09-14 8:00 Michael Wohlwend
2005-09-14 12:01 ` [Caml-list] " Jacques GARRIGUE
0 siblings, 1 reply; 2+ messages in thread
From: Michael Wohlwend @ 2005-09-14 8:00 UTC (permalink / raw)
To: caml-list
Hi,
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.
The other question, does this "apply" function use up the stack? Would it be
better to define a tail-recursive apply outside the class?
cheers
Michael
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] object question
2005-09-14 8:00 object question Michael Wohlwend
@ 2005-09-14 12:01 ` Jacques GARRIGUE
0 siblings, 0 replies; 2+ messages in thread
From: Jacques GARRIGUE @ 2005-09-14 12:01 UTC (permalink / raw)
To: micha-1; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-09-14 12:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-14 8:00 object question Michael Wohlwend
2005-09-14 12:01 ` [Caml-list] " Jacques GARRIGUE
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox