Skip to content

Commit e923165

Browse files
committed
Add some notes reflecting feedback
1 parent 8d7cb43 commit e923165

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

working/0125-static-immutability/feature-specification.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,28 @@ foo<S, T, where T is immutable>(Value<T> v) {
3535
}
3636
```
3737

38-
### Alternative syntax
38+
### Alternative syntax 1
3939

4040
Instead of adding constraints, a simpler approach is to add a marker interface
4141
`Immutable`. The property expressed by the constraint `T is immutable` then
4242
becomes expressed by `implements Immutable` in the case of a class, or `T
4343
extends Immutable` in the case of a type variable `T`.
4444

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+
4560
## Static checking
4661
A class marked with `immutable` is subject to the following additional static
4762
checks.
@@ -56,6 +71,14 @@ checks.
5671
The types `int`, `double`, `bool`, `String`, `Type`, and `Symbol` are considered
5772
immutable.
5873

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+
5982
## Allocation of immutable objects
6083

6184
Immutable objects are allocated as usual in an isolate local
@@ -121,6 +144,16 @@ instance of one of these classes:
121144
Instances that are allocated to initialize fields or top level variables are
122145
always initialized in an umodifiable state.
123146

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+
124157
### Runtime immutability
125158
As with the result of the current `List.unmodifiable` constructor, mutation
126159
operations on an instance of an immutable collection shall throw (except in the
@@ -146,11 +179,38 @@ is not required?
146179
var l = ^[3];
147180
```
148181

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+
149209
## Immutable functions
150210

151211
There is no way to describe the type of an immutable function. If important, we
152212
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.
154214

155215
## Immutable top type
156216

@@ -161,7 +221,10 @@ common super-interface.
161221

162222
## Javascript
163223

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
165227
unlikely to be able to support this in full on the web. It is possible that we
166228
may be able to define a subset of immutable objects which can be implemented as
167229
a layer over shared typed data buffers.
230+

0 commit comments

Comments
 (0)