@@ -6,13 +6,13 @@ Rust's functional roots allow for it to be more expressive in the type system
6
6
than many other languages, and turn many kinds of programming problems into
7
7
"static typing" problems. A key part of this idea is the way generic types work.
8
8
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
11
11
the same boilerplate code for a ` Vec ` type, with two different types filled in.
12
12
13
13
In Rust, the generic type parameter creates what is known as a "type class
14
14
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
16
16
different types* .
17
17
18
18
This is why ` impl ` blocks must specify generic parameters: different ones can
@@ -102,9 +102,9 @@ fn main() {
102
102
Why those chances to panic? Because there are * state invariants* here:
103
103
104
104
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* .
108
108
109
109
It would be possible to add these to the ` Error ` types instead of panicking.
110
110
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
117
117
order in the logic itself.
118
118
119
119
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.
121
123
122
124
Here is what that looks like:
123
125
@@ -227,7 +229,7 @@ fn main() {
227
229
```
228
230
229
231
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.
231
233
232
234
## Disadvantages
233
235
@@ -236,7 +238,7 @@ transitions, an `InvalidState` enum value in an error type might be simpler.
236
238
237
239
## Alternatives
238
240
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:
240
242
241
243
1 . If the state transition is during construction/finalizing of an object, see
242
244
[ Builder Pattern] ( ../patterns/creational/builder.md ) .
0 commit comments