caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* hiding the 'mutable' modifier
@ 2000-02-16 14:05 Thomas Colcombet
  2000-02-18  0:31 ` Markus Mottl
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Colcombet @ 2000-02-16 14:05 UTC (permalink / raw)
  To: caml-list

hello,

I'd like to do something that looks like :

module A =
  (struct
     type t = { mutable field : int }
  end : sig
     type t = { field : int }
  end)

This is not allowed by ocaml, however
I think it would be useful  if the mutable
field is modified, let's say, only during the
construction of terms of type t, but must not
be modified outside the module (for consistency
reasons).
Is there any typing or implementation issue
which prevents such mutable modifier hiding ?

	Thom



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: hiding the 'mutable' modifier
  2000-02-16 14:05 hiding the 'mutable' modifier Thomas Colcombet
@ 2000-02-18  0:31 ` Markus Mottl
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Mottl @ 2000-02-18  0:31 UTC (permalink / raw)
  To: Thomas Colcombet; +Cc: OCAML

> 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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-02-18  9:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-16 14:05 hiding the 'mutable' modifier Thomas Colcombet
2000-02-18  0:31 ` Markus Mottl

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).