Re: hiding the 'mutable' modifier

From: Markus Mottl (mottl@miss.wu-wien.ac.at)
Date: Fri Feb 18 2000 - 01:31:49 MET

  • Next message: Manuel Fahndrich: "variant filtering"

    > 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
    



    This archive was generated by hypermail 2b29 : Fri Feb 18 2000 - 10:48:47 MET