caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques GARRIGUE <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
Date: Sun, 05 Aug 2007 20:55:26 +0900 (JST)	[thread overview]
Message-ID: <20070805.205526.2004154686.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20070805203346.b05112c7.mle+ocaml@mega-nerd.com>

From: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
> 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


  reply	other threads:[~2007-08-05 11:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-02 10:09 Erik de Castro Lopo
2007-08-02 10:29 ` [SPAM?][Caml-list] " Christoph Bauer
2007-08-02 10:42   ` [Caml-list] " Erik de Castro Lopo
2007-08-02 13:19   ` [SPAM?][Caml-list] " skaller
2007-08-05  4:52     ` [Caml-list] " Erik de Castro Lopo
2007-08-05  5:35       ` Erik de Castro Lopo
2007-08-05 10:16       ` skaller
2007-08-05 10:33         ` Erik de Castro Lopo
2007-08-05 11:55           ` Jacques GARRIGUE [this message]
2007-08-05 12:17             ` Erik de Castro Lopo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070805.205526.2004154686.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=mle+ocaml@mega-nerd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).