@@ -35,13 +35,28 @@ foo<S, T, where T is immutable>(Value<T> v) {
35
35
}
36
36
```
37
37
38
- ### Alternative syntax
38
+ ### Alternative syntax 1
39
39
40
40
Instead of adding constraints, a simpler approach is to add a marker interface
41
41
` Immutable ` . The property expressed by the constraint ` T is immutable ` then
42
42
becomes expressed by ` implements Immutable ` in the case of a class, or `T
43
43
extends Immutable` in the case of a type variable ` T`.
44
44
45
+ ### Alternative syntax 2
46
+
47
+ Instead of adding general constraints, we could expose a dedicated syntax. For
48
+ example, this proposal from @yjbanov .
49
+
50
+ ``` dart
51
+ data Value<data T> extends Scalar<T> {
52
+ }
53
+
54
+ foo<S, data T>(Value<T> v) {
55
+ }
56
+
57
+ ```
58
+
59
+
45
60
## Static checking
46
61
A class marked with ` immutable ` is subject to the following additional static
47
62
checks.
@@ -56,6 +71,14 @@ checks.
56
71
The types ` int ` , ` double ` , ` bool ` , ` String ` , ` Type ` , and ` Symbol ` are considered
57
72
immutable.
58
73
74
+ ## Generated methods
75
+
76
+ We may wish to consider automatically generating hashCode and equality methods
77
+ for immutable classes (possibly with caching of hashCode).
78
+
79
+ We may wish to consider automatically generating functional update methods (or
80
+ providing some other form of functional update).
81
+
59
82
## Allocation of immutable objects
60
83
61
84
Immutable objects are allocated as usual in an isolate local
@@ -121,6 +144,16 @@ instance of one of these classes:
121
144
Instances that are allocated to initialize fields or top level variables are
122
145
always initialized in an umodifiable state.
123
146
147
+ Question: Is this functionality needed? With spread collections, many patterns
148
+ will be expressible directly as a literal.
149
+
150
+ Question: Is this sufficient? The analysis as specified is brittle: you cannot
151
+ factor out initialization code into a different scope from the allocation. We
152
+ could add type level support for tracking uninitialized instances, but this
153
+ raises the footprint of this feature substantially.
154
+
155
+ Qustion: Should this functionality be extended to user classes?
156
+
124
157
### Runtime immutability
125
158
As with the result of the current ` List.unmodifiable ` constructor, mutation
126
159
operations on an instance of an immutable collection shall throw (except in the
@@ -146,11 +179,38 @@ is not required?
146
179
var l = ^[3];
147
180
```
148
181
182
+ ### Alternative collection approach
183
+
184
+ Instead of making ` ImmutableList ` a subtype of ` List ` , we could make it either
185
+ an unrelated type, or a supertype of ` List ` .
186
+
187
+ #### ` ImmutableList ` is a supertype
188
+ If ` ImmutableList ` is a supertype of ` List ` , then immutability is no longer type
189
+ based. If we wish to enforce deep immutability, then there would need to be
190
+ runtime checks during initialization, which may be expensive (particularly in
191
+ the case of collections). Alternatively, we could simply not enforce deep
192
+ immutability statically, and instead dynamically traverse an object grap before
193
+ sharing it to check for immutability. This is expensive, but perhaps marginally
194
+ less so than copying.
195
+
196
+ Another downside of this approach is that existing APIs that take ` Lists ` but
197
+ only read them cannot be re-used with an ` ImmutableList ` . A wrapper can help
198
+ with this.
199
+
200
+ A benefit of this is that changing APIs (especially Flutter APIs) to take
201
+ ` ImmutableList ` as an argument would be non-breaking.
202
+
203
+ #### ` ImmutableList ` is an unrelated type
204
+
205
+ If ` ImmutableList ` is unrelated to ` List ` , then we have the same issue with
206
+ re-using existing APIs. However, we retain all of the benefits of type based
207
+ immutability.
208
+
149
209
## Immutable functions
150
210
151
211
There is no way to describe the type of an immutable function. If important, we
152
212
could add a type for immutable closures. A function is immutable if every free
153
- variable of the function is immutable.
213
+ variable of the function is immutable.
154
214
155
215
## Immutable top type
156
216
@@ -161,7 +221,10 @@ common super-interface.
161
221
162
222
## Javascript
163
223
164
- Currently, isolates are not supported in Javascript. If we revisit that, we are
224
+ There are no issues with supporting immutable objects on the web, but the
225
+ ability to support communication between isolates is limited. Currently,
226
+ isolates are not supported at all in Javascript. If we revisit that, we are
165
227
unlikely to be able to support this in full on the web. It is possible that we
166
228
may be able to define a subset of immutable objects which can be implemented as
167
229
a layer over shared typed data buffers.
230
+
0 commit comments