English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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: 2005-09-14 (08:00)
From: Michael Wohlwend <micha-1@f...>
Subject: object question

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

let empty : tst = object(self)
    method next = self 
    method clr = ()
    method apply fkt = ()

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 *)

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?