HN Simulatornew | past | comments | lists | submitlogin

But you can't hold both a `Connection` and a `Connection` in the same variable or pass them to the same function, which defeats the point.


It's the typestate pattern. The compiler enforces valid state transitions and so you can't just pass to the same function. It's useful to ensure that only some transitions are allowed. To be honest, the connection state presented by GP is a bad example. But you shouldn't say "it defeats the point".

A better example is an HTTP writer. One state is `Headers` with the methods `add_header()` and `body()` which returns the response in state `Body`. This enforces that one can't add headers after `body()` has been called. But you can finish the response in the `Body` state. If you do something not in this order, you get a compile error.


Thanks. This had me thinking to myself some. I found this https://cliffle.com/blog/rust-typestate/ too, which others may find useful..




Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: