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.4 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 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 8A701BC6C for ; Sun, 5 Aug 2007 13:55:33 +0200 (CEST) Received: from rabbit.math.nagoya-u.ac.jp (rabbit.math.nagoya-u.ac.jp [133.6.130.5]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l75BtVNE031895 for ; Sun, 5 Aug 2007 13:55:32 +0200 Received: from localhost (rabbit-172 [172.16.254.254]) by rabbit.math.nagoya-u.ac.jp (8.12.11/3.7W) with ESMTP id l75BtRAt021674; Sun, 5 Aug 2007 20:55:27 +0900 (JST) Date: Sun, 05 Aug 2007 20:55:26 +0900 (JST) Message-Id: <20070805.205526.2004154686.garrigue@math.nagoya-u.ac.jp> To: mle+ocaml@mega-nerd.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Handling include files using ocamllex From: Jacques GARRIGUE In-Reply-To: <20070805203346.b05112c7.mle+ocaml@mega-nerd.com> References: <20070805145238.76aed5d6.mle+ocaml@mega-nerd.com> <1186308994.9925.132.camel@rosella.wigram> <20070805203346.b05112c7.mle+ocaml@mega-nerd.com> X-Mailer: Mew version 4.2 on Emacs 21.4 / 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 46B5BAB3.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocamllex:01 ocaml:01 val:01 mutable:01 val:01 mutable:01 lexbuf:01 lexing:01 vouillon:01 lexbuf:01 lexing:01 notation:01 jerome:01 caml-list:01 immutable:01 From: Erik de Castro Lopo > Well the problem was that I wanted to do this: > > class lexstack top_filename > object > val mutable filename = top_filename > val mutable chan = open_in top_filename > val lexbuf = Lexing.from_channel chan > > Oops, error message right there ^^^^^^ trying to use instance > variable chan. 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.) Jacques Garrigue