Skip to content

Commit f7fc3fe

Browse files
simonsanjhwgh1968
authored andcommitted
Changes from review
1 parent 8511ba8 commit f7fc3fe

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

functional/stateful-types.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Rust's functional roots allow for it to be more expressive in the type system
66
than many other languages, and turn many kinds of programming problems into
77
"static typing" problems. A key part of this idea is the way generic types work.
88

9-
In C++ and Java, for example, generic types a meta-programming construct for the
10-
compiler. A `Vec<int>` and `Vec<char>` in C++ are just two different copies of
9+
In C++ and Java, for example, generic types are a meta-programming construct for
10+
the compiler. `Vec<int>` and `Vec<char>` in C++ are just two different copies of
1111
the same boilerplate code for a `Vec` type, with two different types filled in.
1212

1313
In Rust, the generic type parameter creates what is known as a "type class
1414
constraint", and each value used by an end user *actually changes the type of
15-
each instatiation*. In other words, `Vec<usize>` and `Vec<char>` *are two
15+
each instatiation*. In other words, `Vec<usize>` and `Vec<char>` *are two
1616
different types*.
1717

1818
This is why `impl` blocks must specify generic parameters: different ones can
@@ -102,9 +102,9 @@ fn main() {
102102
Why those chances to panic? Because there are *state invariants* here:
103103

104104
1. `set_param` can only be called in an "initial" state.
105-
1. `init` must be called before any scripts are compiled.
106-
1. `exec` can only be called in a "loaded" or "ready" state.
107-
1. `init` must be called *exactly once*.
105+
2. `init` must be called before any scripts are compiled.
106+
3. `exec` can only be called in a "loaded" or "ready" state.
107+
4. `init` must be called *exactly once*.
108108

109109
It would be possible to add these to the `Error` types instead of panicking.
110110
However, this solution is suboptimal. Not only would users of the code
@@ -117,7 +117,9 @@ if it were misused. After all, every user's program contains the invalid call
117117
order in the logic itself.
118118

119119
In Rust, this is actually possible! The solution is to *change the type* in
120-
order to enforce the invariants. How? With a private generic parameter.
120+
order to enforce the invariants.
121+
122+
How? With a private generic parameter.
121123

122124
Here is what that looks like:
123125

@@ -227,7 +229,7 @@ fn main() {
227229
```
228230

229231
They would get a syntax error. The type `Interpreter<Loaded>` does not
230-
implement set param, only the type `Interpreter<Init>` does.
232+
implement `set_param()`, only the type `Interpreter<Init>` does.
231233

232234
## Disadvantages
233235

@@ -236,7 +238,7 @@ transitions, an `InvalidState` enum value in an error type might be simpler.
236238

237239
## Alternatives
238240

239-
There are a number of simpler state machines, however, that have their own patterns:
241+
There are a number of simpler state machines that have their own patterns:
240242

241243
1. If the state transition is during construction/finalizing of an object, see
242244
[Builder Pattern](../patterns/creational/builder.md).

0 commit comments

Comments
 (0)