Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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 :
    sig
      type t
      val field : t -> int
    end =

    struct
      type t = { mutable field : int }

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

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.

Regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl