summaryrefslogtreecommitdiff
path: root/chapter17/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'chapter17/README.md')
-rw-r--r--chapter17/README.md97
1 files changed, 92 insertions, 5 deletions
diff --git a/chapter17/README.md b/chapter17/README.md
index 5a7f48d3..95cc397e 100644
--- a/chapter17/README.md
+++ b/chapter17/README.md
@@ -1,6 +1,7 @@
# Chapter 17: Syntax Sugar
-# Post-increment
+
+## Post-increment
Allow `arg++` and `arg--` with the usual meanings; the value is updated and the
expression is the pre-update value. Greedy match is used so `arg---arg` parses
@@ -8,12 +9,13 @@ as `(arg--)-arg`.
Also allows `s.fld++` and `ary[idx]++`, and their `--` variants.
-# var & val
+
+## var & val
`var` can be used in the `type` position to indicate a "variable" (mutable
-value), whose type will be inferred from the initalizing expression.
+value), whose type will be inferred from the initalizing expression.
-`val` is the same as `var`, except its a "value" (not mutable).
+`val` is the same as `var`, except it is a "value" (not mutable).
The inferred value is the `glb` of the peephole type, which means `var` and
`val` will not infer types like `u8` or `f32`, instead inferring `int` and
@@ -26,9 +28,91 @@ infers as
`S s? = new S;`
+## Initialized fields are immutable by default
+
+Typed field declarations with an initializer are immutable by default, and can
+be made mutable with a leading `!`. `var` and `val` keep their current sense.
+Fields are immutable at the end of either constructor, but can be changed
+during construction.
+
+
+Examples:
+
+
+```java
+struct Point { int x,y; }; // Fields x and y are not initialized, mutable
+Point p = new Point;
+p.x++; // Mutate is ok
+return p.x;
+```
+
+```java
+struct Point { int x=3,y=4; }; // Fields x and y are initialized, immutable
+Point p = new Point;
+p.x++; // Error!
+return p.x;
+```
+
+```java
+struct Point { int x=3,y=4; }; // Fields x and y are initialized, immutable
+Point p = new Point { x=x*x; y=y*y; }; // Ok to mutate in the constructor
+p.x++; // Error!
+return p.x;
+```
+
+```java
+struct Point { int !x=3, !y=4; }; // Fields x and y are initialized, mutable
+Point p = new Point;
+p.x++; // Mutate is OK
+return p.x;
+```
+
+```java
+struct Point { var x,y; }; // Error, var and val require an initializer
+Point p = new Point;
+p.x++;
+return p.x;
+```
+
+```java
+struct Point { var x=3,y=4; }; // Fields x and y are mutable
+Point p = new Point;
+p.x++; // Mutate is OK
+return p.x;
+```
+
+```java
+struct Point { val x=3,y=4; }; // Fields x and y are immutable
+Point p = new Point;
+p.x++; // Error!
+return p.x;
+```
+
+
+## Deep Immutability (proposal)
+
+```cpp
+struct Bar { int x; };
+Bar b = new Bar; /* b = null; *//* b.x++; */ // immutable & deep immutable
+Bar !b = new Bar; b = null; /* b.x++; */ // mutable & deep immutable
+Bar b! = new Bar; /* b = null; */ b.x++; // immutable & deep mutable
+Bar !b! = new Bar; b = null; b.x++; // mutable & deep mutable (ignoring NPE)
+val b = new Bar; /* b = null; *//* b.x++; */ // immutable & deep immutable
+var b = new Bar; b = null; b.x++; // mutable & inherits deep mutability from RHS
+```
+
+
+## Trinary
+
+Allow `pred ? e_true : e_false`. Also allow `pred ? e_true`, where the false result
+is the zero type version of the true result.
+
+
+
+
+
-invert !
C++-style for-loop
for-interator
trinary
@@ -36,6 +120,9 @@ switch
+
+
+
# Chapter 18: Functions
Some thinking on function syntax: