ECMA proposal would bring records and tuples to JavaScript

A technical committee has proposed adding new deeply immutable value types to the ECMAScript standard

ECMA proposal would bring records and tuples to JavaScript
Iwan Gabovitch (CC BY 2.0)

JavaScript would gain record and tuple value types under a proposal before ECMA International, the standards body that oversees the popular programming language for web development.

Records and tuples would introduce two deeply immutable data structures to JavaScript: Record, an object-like structure, and Tuple, an array-like structure. A draft of the plan with ECMA Technical Committee 39, which governs ECMAScript, the standard underlying JavaScript.

Records and tuples, the proposal states, can only contain primitives and other records and tuples. They can be thought of as “compound primitives.” And by being thoroughly based on primitives, they are deeply immutable. Like objects and arrays, records and tuples support comfortable idioms for construction, use, and manipulation, the proposal states. They are compared by contents rather than by identity.

JavaScript engines might perform certain optimizations on construction, manipulation, and comparison of records and tuples, analogous to how strings are implemented. Records and tuples are intended to be usable and understood with external type system supersets such as TypeScript or Flow.

Currently, userland libraries such as immutable.js implement similar concepts. A previous ECMA proposal for immutable data structures was attempted but abandoned because of complexity and lack of sufficient use cases, the proposal states. The new proposal introduces significant changes, offering usability advantages over userland libraries such as the following:

  • Records and tuples are easily introspectable in a debugger.
  • No additional branching is needed to write a generic library that consumes immutable and JS objects.
  • Use cases are avoided where developers may expensively convert between regular JS objects and immutable structures.

Records and tuples interoperate well with objects and arrays. They can be read the same way as objects and arrays. The key difference centers on deep immutability and comparison by value rather than identity. Also, the proposed syntax significantly improves the ergonomics of using Record and Tuple in code.

The most recent version of the ECMAScript specification, version 2020, was adopted in June. It featured capabilities in areas such as module loading and a new BigInt type.

Copyright © 2020 IDG Communications, Inc.