From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 6882EBC69 for ; Sun, 5 Aug 2007 14:17:59 +0200 (CEST) Received: from smtp.syd.people.net.au (smtp.syd.people.net.au [218.214.225.98]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l75CHuPa003520 for ; Sun, 5 Aug 2007 14:17:58 +0200 Received: (qmail 24118 invoked from network); 5 Aug 2007 12:18:13 -0000 Received: from unknown (HELO hendrix.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 5 Aug 2007 12:18:13 -0000 Received: from hendrix (hendrix [192.168.200.99]) by hendrix.mega-nerd.net (Postfix) with SMTP id B6C5C2FEA1 for ; Sun, 5 Aug 2007 22:17:55 +1000 (EST) Date: Sun, 5 Aug 2007 22:17:55 +1000 From: Erik de Castro Lopo To: caml-list@inria.fr Subject: Re: [Caml-list] Handling include files using ocamllex Message-Id: <20070805221755.6546489f.mle+ocaml@mega-nerd.com> In-Reply-To: <20070805.205526.2004154686.garrigue@math.nagoya-u.ac.jp> References: <20070805145238.76aed5d6.mle+ocaml@mega-nerd.com> <1186308994.9925.132.camel@rosella.wigram> <20070805203346.b05112c7.mle+ocaml@mega-nerd.com> <20070805.205526.2004154686.garrigue@math.nagoya-u.ac.jp> Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed 2.3.1 (GTK+ 2.10.11; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 46B5BFF4.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ocamllex:01 vouillon:01 lexbuf:01 mutable:01 val:01 mutable:01 val:01 lexbuf:01 lexing:01 lexing:01 notation:01 ocaml:01 perlis:01 wrote:01 Jacques GARRIGUE wrote: > Interesting, because the example you describe here is precisely the > reason it is not allowed (at least as Jerome Vouillon explained to > me.) > That is, you intend the instance variable lexbuf to be the one > associated to the current (mutable) chan, but if you change chan this > will no longer be true. > So, in order to avoid this kind of ambiguity, you have to use let > defined variables. For instance: > > class lexstack top_filename = > let init_chan = open_in top_filename in > object > val mutable filename = top_filename > val mutable chan = init_chan > val mutable lexbuf_chan = init_chan > val mutable lexbuf = Lexing.from_channel init_chan > method lexbuf = > if chan == lexbuf_chan then lexbuf else > (lexbuf <- Lexing.from_channel chan; lexbuf_chan <- chan) > ... > > Note that this restriction applies also to immutable instance > variables, because you can modify them through functional update > (the {< chan = ... >} notation.) Thanks for the excellent explanation Jacques. I must admit, this is one of my first real forays into OO side of ocaml, but I quickly found that my aims could be acheived just as easily using the technique I posted. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "A programming language is low level when its programs require attention to the irrelevant." -- Alan Perlis