3 ms·
Type checking doesn't require classes. You can typecheck a funtion. I am not talking about type checking a function. I am talking about type checking data stru
by nerdponx 6y ago
Type checking doesn't require classes. You can typecheck a funtion.
I am not talking about type checking a function. I am talking about type checking data structures, replacing this:
record = { "a": 1, "b": 2 }
with this:
record = Record(a=1, b=2)
The latter can be statically type checked much more easily than the former.
This is analogous to Scala case classes and Haskell records. And it can be a significant productivity improvement in big codebases, because it reduces your reliance on documentation and working memory. Instead, it lets you depend on the type checker and IDE to provide auto-completion to prevent a whole class of errors.
In fact, it lets you write less runtime code and have shorter, simpler code paths in some cases, because you can mostly skip checks like "if x is an int" (at least in internal/private functions), and even skip the corresponding unit tests, and let the type checker deal with it instead.
Edit: as for the example in the article... really? This is just single dispatch. What would you write instead, one big if/else statement? A dict of functions to dispatch to? `AstNode` is an interface, and all of these subclasses implement the interface.