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
Handling include files using ocamllex
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-05 (11:55)
From: Jacques GARRIGUE <garrigue@m...>
Subject: Re: [Caml-list] Handling include files using ocamllex
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
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
    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