Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
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