avatarMatan Lurey

Summary

This article introduces immutability in Dart, discussing the use of the const modifier for creating immutable objects and the role of the package:meta for further static checking to enforce deep immutability.

Abstract

The article "An intro to immutability with Dart" delves into the significance of immutability in programming, particularly in Dart. It explains how Dart's const modifier and const constructors create both immutable bindings and objects, distinguishing it from ES6's const, which only provides immutable bindings. The article emphasizes that all literals in Dart can be const and that user-defined types can also have const constructors, ensuring deep immutability and canonicalization at compile time. Additionally, it introduces the "immutable" annotation from package:meta, which aids developers in maintaining deeply immutable classes at runtime, though without compile-time canonicalization. The author encourages readers to explore the Dart language tour for more details and invites feedback for further discussion on Dart and immutability.

Opinions

  • The author believes that immutability is crucial in programming, as it simplifies reasoning about data.
  • Dart's approach to immutability with const is highlighted as superior to ES6's const due to its enforcement of immutability at the language level.
  • The article suggests that Dart's const not only ensures immutability but also optimizes runtime performance by canonicalizing instances.
  • The introduction of the "immutable" annotation from package:meta is presented as a valuable tool for developers to ensure the immutability of their classes when const is too restrictive.
  • The author is open to community engagement and interested in furthering the discussion on Dart and immutability, indicating a commitment to community-driven learning and improvement.

An intro to immutability with Dart

It’s impossible to deny that immutability is a hot topic in programming, especially front-end programming. Libraries like Immutable.js and other concepts like unidirectional data flow have argued it’s easier to reason about data when it doesn’t change underneath you:

In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created.

Well, what about Dart? We have a couple concepts that lend themselves well to immutability and immutable objects built-in, starting with the const modifier and const constructor. Not to be confused with const in ES6, which is merely an immutable binding:

In Dart, const is both an immutable binding and an immutable object:

All literals (Null, String, int, double, num, bool, Map, List, Symbol) are capable of being const, and it’s possible to create user-types with a const constructor:

Let’s review — const instances are both immutable bindings and enforced at a language level to be both deeply immutable — and also are canonicalized at compile-time — that is, any two instances are considered equivalent, and are only represented by a single instance when running. For example, the following is rather cheap — it only allocates a single instance at runtime:

Want to learn more? Read the Dart language tour about final and const.

Further static checking with package:meta

Of course, const is a bit restrictive — you must be able to create a class at compile-time — so you couldn’t for example read a database and create const objects at runtime. We’ve recently introduced the “immutable” annotation with package:meta:

You can use this annotation to help enforce that developers keep your classes deeply immutable. It won’t be canonicalized like const, but can still be quite helpful for developers.

I hope this was a nice introduction to immutability. Please let me know in the comments or on twitter if you’d like to learn more about Dart or immutability.

JavaScript
Functional Programming
Dart
Dartlang
Immutable
Recommended from ReadMedium