On Mon, May 31, 2021 at 4:05 AM Shiro Kawai <xxxxxx@gmail.com> wrote:I think we can do better. One idea is this:
- The data structure has a mutability flag internally.
- The functional interface always returns immutable data. It may return the input as is, or return a structure that partially shares the input, if the input structure is flagged immutable. If the input is flagged mutable, it always returns a fleshly copied immutable structure.
- The linear-updating interface may mutate the input structure if it is flagged mutable, and copies if the input structure is flagged immutable.The HAMT APIs that underlie parts of the sample implementation of SRFI 146 (see Github) use a similar interface. When an instance of the data structure is set to be mutable, it also uses a slightly different representation that facilitates updates. When the mutability flag is cleared, the data structure is copied, and the representation switches back. There's never any danger of mutating an object that has been set to be immutable. I got the idea from the Clojure APIs.