Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: hiding the 'mutable' modifier
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-02-18 (09:44)
From: Markus Mottl <mottl@m...>
Subject: Re: hiding the 'mutable' modifier
> I'd like to do something that looks like :
> module A =
>   (struct
>      type t = { mutable field : int }
>   end : sig
>      type t = { field : int }
>   end)

The usual idea of a module is to hide the concrete implementation of some
data type and to provide for functions that only allow the kind of access
you want.

So you could write, e.g.:

  module A :
      type t
      val field : t -> int
    end =

      type t = { mutable field : int }

      let field x = x.field
      let internal_only x n = x.field <- n

As you can see, the signature to which "A" is restricted, does not show how
"t" is implemented (mutable/nonmutable, whatever).

It only provides for a function "field" that allows read-only access to the
field in type t. However, the user of this module would not (and need not)
even know that "t" is implemented as a record with a field of this name.
He would only have to write "field foo", for example, to get the
information needed.

Function "internal_only" destructively updates the record field, but it is
not visible to the outside, because it does not appear in the signature.

Markus Mottl

Markus Mottl,,