简体   繁体   中英

Why are Reason Arrays Mutable?

I suppose I am asking about the rationale behind this design decision.

Reason arrays being mutable sticks out as an aberration amongst its other data structures (list, record, hashmap, set) that are immutable by default.

Is there a reason for this? Is there an immutable alternative?

There's really no such thing as "Reason arrays". Reason is an alternative syntax to OCaml, and OCaml has mutable arrays. Reason is however typically used with the BuckleScript back-end that compiles to JavaScript, which also has mutable arrays, but the reason why are slightly different.

  • In OCaml proper, arrays are used when you want the characteristics of an array, typically for its performance profile, but you might also want to use it for its memory layout which is very straightforward and easy to interact with from other languages, but also necessary to be able to communicate with hardware through the access and mutation of shared address spaces.

  • With BuckleScript, arrays map straight to JavaScript arrays, which in addition to being mutable is also dynamically sized and so effectively an array-list. Here too, you'd typically use it for performance and interoperation with JavaScript, but because the implementation is slightly different, the performance characteristics are subtly different as well.

In both cases, if you want something array-like but immutable you'd typically use a list, but there are other options as well, such as Immutable.re's Vector .

It might be better to ask why OCaml doesn't also include an immutable array data type in its standard library. I'm not sure anyone can give a definite answer to that, but it might just be because it's not been requested enough, perhaps because list does the immutable array-like thing well enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM