A universal pattern for less wrong™ code
2024-02-28
Alex Povel
can be in one of the following states:
states...
whole classes of errors disappear
class
some are still possible:
class WaitingProcess(Process): @classmethod def from_initiating_io(cls, process: ReadyProcess) -> t.Self: return cls(id=process.id)
unit testing partly obsolete
first-class tooling and IDE support
u32
typestate_irl
again, some impossible states become impossible to represent
structured programming is great and all, but can be mifused:
if
try lick_elbow(): if not is_elbow_licked(): for elbow in elbows: if elbow.is_lickable(): lick(elbow) if is_monday() else make_breakfast() raise ElbowNotLickedError # ...
maps to database neatly, e.g. one type per table in ORM
VerifiedEmailAddress
https://github.com/alexpovel/effective-typestate
* ... works in most languages (OOP, FP, ...)