From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 92BC6BB9C for ; Wed, 14 Sep 2005 14:02:04 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j8EC24Ys016448 for ; Wed, 14 Sep 2005 14:02:04 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA00324 for ; Wed, 14 Sep 2005 14:02:03 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j8EC21n5030628 for ; Wed, 14 Sep 2005 14:02:02 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j8EC1u4l025898; Wed, 14 Sep 2005 21:01:56 +0900 (JST) Date: Wed, 14 Sep 2005 21:01:56 +0900 (JST) Message-Id: <20050914.210156.68535209.garrigue@kurims.kyoto-u.ac.jp> To: micha-1@fantasymail.de Cc: caml-list@inria.fr Subject: Re: [Caml-list] object question From: Jacques GARRIGUE In-Reply-To: <200509141000.50364.micha-1@fantasymail.de> References: <200509141000.50364.micha-1@fantasymail.de> X-Mailer: Mew version 4.0.64 on Emacs 21.2 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4328113C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 43281139.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 val:01 mutable:01 stack:01 tail-call:01 ...:98 ...:98 functions:01 define:01 jacques:01 jacques:01 defined:01 unit:03 unit:03 garrigue:03 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 From: Michael Wohlwend > 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