Skip to content

Commit 08138c5

Browse files
committed
Some modification suggestions
1 parent 3dc3f59 commit 08138c5

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

content/post/2024-11-26-typed-fsm.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ date: 2024-11-26T13:32:00+08:00
1717

1818
它具有以下两点优势:
1919
### 1. 类型安全,极大方便代码的编写,修改和重构
20-
手写状态机在实际代码中有很大的心智负担,而对于它的修改和重构则更加艰难
20+
手写状态机在实际代码中有很大的心智负担,对于它的修改和重构更是如噩梦一样
2121

2222
typed-fsm-zig在类型上跟踪状态机的变化,使消息的定义,产生,处理都和状态相关联,从而让类型系统帮我们检查这个过程中是否存在状态错误。
2323

@@ -200,16 +200,16 @@ pub fn main() !void {
200200
_ = s1Handler(s1Wit, &val);
201201
}
202202

203-
pub fn Witness(T: type, b: T, a: T) type {
203+
pub fn Witness(T: type, end: T, start: T) type {
204204
return struct {
205-
pub fn getMsg(self: @This()) @TypeOf(a.STM(b).getMsg) {
206-
if (b == a) @compileError("Can't getMsg!");
205+
pub fn getMsg(self: @This()) @TypeOf(start.STM(end).getMsg) {
206+
if (end == start) @compileError("Can't getMsg!");
207207
_ = self;
208-
return a.STM(b).getMsg;
208+
return start.STM(end).getMsg;
209209
}
210210

211211
pub fn terminal(_: @This()) void {
212-
if (b != a) @compileError("Can't terminal!");
212+
if (end != start) @compileError("Can't terminal!");
213213
return {};
214214
}
215215
};
@@ -220,11 +220,11 @@ const Exmaple = enum {
220220
s2,
221221

222222
// State to Message union
223-
pub fn STM(s: Exmaple, b: Exmaple) type {
224-
return switch (s) {
225-
.exit => exitMsg(b),
226-
.s1 => s1Msg(b),
227-
.s2 => s2Msg(b),
223+
pub fn STM(start: Exmaple, end: Exmaple) type {
224+
return switch (start) {
225+
.exit => exitMsg(end),
226+
.s1 => s1Msg(end),
227+
.s2 => s2Msg(end),
228228
};
229229
}
230230
};
@@ -280,21 +280,21 @@ fn s2Handler(val: Witness(Exmaple, .exit, .s2), ref: *i32) void {
280280
281281
感兴趣的可以看一下,看懂这些要求你了解GADT,上面提到的Mcbirde Indexed Monad 本质就是在GADT类型上的monad。
282282
283-
在这里的Winess 三个参数T表示状态机的类型,b 表示终止时的状态,a,表示当前的状态。
283+
在这里的Winess 三个参数T表示状态机的类型,end 表示终止时的状态,start,表示当前的状态。
284284
它有两个函数,getMsg 表示从外部获取消息的函数,terminal 表示终止状态机的函数。
285285
286-
当b==a时表示当前处于终止状态,因此Witness只能使用terminal函数,当b!=a时表示当前不处于终止状态,应该继续从外部获取消息,因此Witness只能使用getMsg函数。
286+
当end == start时表示当前处于终止状态,因此Witness只能使用terminal函数,当end != start时表示当前不处于终止状态,应该继续从外部获取消息,因此Witness只能使用getMsg函数。
287287
```c
288-
pub fn Witness(T: type, b: T, a: T) type {
288+
pub fn Witness(T: type, end: T, start: T) type {
289289
return struct {
290-
pub fn getMsg(self: @This()) @TypeOf(a.STM(b).getMsg) {
291-
if (b == a) @compileError("Can't getMsg!");
290+
pub fn getMsg(self: @This()) @TypeOf(start.STM(end).getMsg) {
291+
if (end == start) @compileError("Can't getMsg!");
292292
_ = self;
293-
return a.STM(b).getMsg;
293+
return start.STM(end).getMsg;
294294
}
295295

296296
pub fn terminal(_: @This()) void {
297-
if (b != a) @compileError("Can't terminal!");
297+
if (end != start) @compileError("Can't terminal!");
298298
return {};
299299
}
300300
};
@@ -316,11 +316,11 @@ const Exmaple = enum {
316316
s2,
317317

318318
// State to Message union
319-
pub fn STM(s: Exmaple, b: Exmaple) type {
320-
return switch (s) {
321-
.exit => exitMsg(b),
322-
.s1 => s1Msg(b),
323-
.s2 => s2Msg(b),
319+
pub fn STM(start: Exmaple, end: Exmaple) type {
320+
return switch (start) {
321+
.exit => exitMsg(end),
322+
.s1 => s1Msg(end),
323+
.s2 => s2Msg(end),
324324
};
325325
}
326326
};

0 commit comments

Comments
 (0)