Skip to content

Private arrays #8927

@mjambon

Description

@mjambon

I have a use case where private arrays would be useful. The use case is similar to uses of private records with mutable fields. It allows the user to read certain details without being able to mutate the data structure. Only the module in charge of creating and maintaining the data structure is allowed to modify it (or create it).

For example, I have a module that exposes an array of names:

type t
val names : t -> string array

The names function returns the same physical array every time. It would nice to ensure the user won't try to change the names by assigning to this array. It would also be nice to achieve this without having to create a custom read-only interface to the array, such as val name : t -> int -> string because it's not clear to the user how efficient this is. In such case we could add documentation explaining name t i is just a read from an array, but it's not as clear as having names.(i) right in the user's code.

So, I think it would be nice to have private support for arrays, like this:

type t
type names = private string array
val names : t -> names

I don't know the technical difficulties involved. For me it's merely a wish for a nice-to-have feature, and it seems consistent with the behavior of private records with mutable fields (which I already use generously).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions