Allow mutable fields of private records

Hi everyone,

The private keyword (unfortunately undocumented) allows you to expose the underlying type of a type while protecting instantiation. This feature is particularly useful for bindings.

Among other types you can apply it to records, this is a nice middle-ground between a classic record and a fully abstract type.

There is today a limitation though, you can’t mutate a mutable field of a private record, for example:

module Foo: {
  @allowMutation
  type t = private {
    mutable foo: int,
  }
  let make: int => t
} = {
  type t = {
    mutable foo: int,
  }
  let make = foo => {foo: foo}
}

let foo = Foo.make(10)

foo.foo = 5 // this would error today: Cannot assign field foo of the private type Foo.t

We’re considering lifting this limitation. Has anybody anything against this?

Thanks for your feedback

ReScript maintainers

2 Likes

Maybe it can be converted into a new warn-error number? So still erroring by default, but with the possibility to opt-out?

Good idea, this way you could decide the behavior you’d like to have.