diff options
| author | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-03 14:03:48 -0700 |
| commit | 22df891893d30e9906d0b14fc3ae5d8e67d500b2 (patch) | |
| tree | 2bda142f92febc8b26f1cabd4bb0c46d1864f34b /chapter16/README.md | |
| parent | c3958e34c0de21df52e57e47e4f47ffe6464f138 (diff) | |
| download | Simple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.tar.gz Simple-22df891893d30e9906d0b14fc3ae5d8e67d500b2.zip | |
Chapter renumber, and split ch 17
Renumber chapters 10a, 10b, 11, 12, 13, 14 -> 10, 11, 13, 12, [11,14].
Split ch 17 into a 17a and 17b; shuffle around im/mutable syntax to be consistent, and present it more slowly.
THe new immutable syntax is pushed through all later chapters
Diffstat (limited to 'chapter16/README.md')
| -rw-r--r-- | chapter16/README.md | 54 |
1 files changed, 13 insertions, 41 deletions
diff --git a/chapter16/README.md b/chapter16/README.md index 663f175a..c55de47f 100644 --- a/chapter16/README.md +++ b/chapter16/README.md @@ -1,25 +1,25 @@ -# Chapter 16: Constructors and Final fields +# Chapter 16: Constructors [Previous: Chapter 15](../chapter15/README.md) | -[Next: Chapter 17](../chapter17/README.md) +[Next: Chapter 17a](../chapter17a/README.md) # Table of Contents 1. [Constructors](#constructors) 2. [Initialization code](#initialization-code) -3. [Final fields](#final-fields) -4. [Multiple Decls](#multiple-declarations-of-the-same-type) -5. [Memory through constructors](#memory-through-constructors) +3. [Multiple Decls](#multiple-declarations-of-the-same-type) +4. [Memory through constructors](#memory-through-constructors) -In this chapter, we add constructors and final fields. +This chapter adds field defaults and constructor blocks. Chapter 17a will add +fixed bindings and read-only access, using these constructors to initialize them. You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter16) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter15...linear-chapter16) it to the previous chapter. ## Constructors -A big problem with the [Chapter 13](../chapter13/README.md) refs is that +A big problem with the [Chapter 12](../chapter12/README.md) refs is that not-null fields always start out `null`. This is fixed in this chapter, where -a *constructor* syntax is required to initialize `final` and not-null fields. +a *constructor* syntax is required to initialize not-null fields. Fields are initialized in three ways: @@ -46,7 +46,7 @@ struct Point { int x=1; int y=1; }; // Point x and y will start as 1, not 0 return new Point { x=3; }.x; // Returns a 3 ``` -Not-null and final fields *must* be initialized before first use and before the +Not-null fields *must* be initialized before first use and before the end of the allocation. They can be initialized in either the declaration or allocation. They do not start with the default value, although the initialization can be to the default. @@ -81,6 +81,8 @@ struct Square { return new Square; ``` +Another example: + ``` struct Buffer { if( arg < 0 || arg > 1000000 ) @@ -91,32 +93,6 @@ return new Buffer; ``` -## Final fields - -Final fields are declared with a `!` before the name. -This decision is revisited in the next chapter. - -``` -int !x = 17; // Final field x -x = 3; // ERROR: cannot re-assign a final field -``` - -``` -struct Person { u8[]? !name; }; -return new Person { name = null; }; // OK: final name is assigned -``` - -``` -struct Point { int !x; int !y; int !z; }; -return new Point { x=arg; y=z; z=x; }; // ERROR: 'Point' is not fully initialized, field 'y' needs to be set in a constructor -``` - -``` -struct Point { int x; int y; int z; }; // Fields not-final, default to 0 -return new Point { x=arg; y=z; z=x; }; // Ok: x=arg, y=0, z=arg -``` - - ### Multiple declarations of the same type Also new this chapter is allowing multiple declarations with the same type, as @@ -127,16 +103,12 @@ int x,y; // Two int variables declared struct Point { int x,y,z; }; // Three fields declared ``` -``` -int !x=3,!y=5; // Two int variables declared, both are final and initialized -``` ## Memory through constructors -The lazy memory partitioning from [Chapter 10b](../chapter10b/README.md) continues +The lazy memory partitioning from [Chapter 11](../chapter11/README.md) continues through constructors. The parser tracks one `$mem` variable alongside its scalar variables. Branches and loops merge that binding with BulkMemPhi; field access -splits out precise MemPhis when needed. The variable records for types and final -fields live in ScopeNode. There is no separate parser table of memory aliases. +splits out precise MemPhis when needed. The variable records for declared types live in ScopeNode. There is no separate parser table of memory aliases. A constructor computes its field values before the allocation. Reads, writes, and loops in its body update the same `$mem` binding as ordinary code. New then |
