Version française
Home     About     Download     Resources     Contact us    
Browse thread
object question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Wohlwend <micha-1@f...>
Subject: object question
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