From b893f6389a95453390686890243463baa441beac Mon Sep 17 00:00:00 2001 From: Lars <890725+larssn@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:53:14 +0200 Subject: [PATCH 1/2] docs: Added the 6th rule of immutable classes So with Dart 3, we can now safely update the section about it being impossible to create `final` classes in Dart. --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cd976d3..f69245c 100644 --- a/README.md +++ b/README.md @@ -1996,13 +1996,8 @@ To create an immutable object **in Dart** you must follow these 5 rules: changes have no external effects (this may be useful for lazy initialization, caching and improving performance). -_Note:_ There should also be a 6th rule stating that the class should be `final` (in the Java sense) -, but in Dart it's impossible to prevent a class from being subclassed. The problem is that one can -always subclass an otherwise immutable class and then add mutable fields to it, as well as override -public methods to return changed values according to those mutable fields. This means that in Dart -it's impossible to create strictly immutable classes. However, you can make it as close as possible -to the real thing by at least not invoking overridable methods from the constructor (which in Dart -means not invoking public non-static methods). +6. Make classes `final` or `sealed` **(Dart 3+ only)** to prevent classes outside the current library to subclass it. + The prevents subclasses adding mutable fields. It also prevents the Fragile base class problem.
From dd9b8e5730f339c34b027e6d40b5d0fb95ccdab1 Mon Sep 17 00:00:00 2001 From: Lars <890725+larssn@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:56:12 +0200 Subject: [PATCH 2/2] docs: fix spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f69245c..61e65ea 100644 --- a/README.md +++ b/README.md @@ -1997,7 +1997,7 @@ To create an immutable object **in Dart** you must follow these 5 rules: improving performance). 6. Make classes `final` or `sealed` **(Dart 3+ only)** to prevent classes outside the current library to subclass it. - The prevents subclasses adding mutable fields. It also prevents the Fragile base class problem. + This prevents subclasses adding mutable fields. It also prevents the Fragile base class problem.