Fork me on GitHub
David Pham19:04:39

Some says a class of errors disappear with static typing. (Which are they?) Can static typing also introduce errors?

Jivago Alves19:04:53

Some languages won’t allow a NPE to happen just because they force you to handle all possible cases. This is just an example. You can have errors on your logic and that’s what tests are for in those languages. Depending on how rich is the type system, you can make illegal states impossible. In other words, code won’t even compile.


hope this doesn’t come off as trite, but static typing mostly prevents type errors at runtime. static typing shouldn’t introduce errors, but it is often more verbose and may require you to change perfectly valid code to appease the type checker. changing your code to appease the type checker may introduce errors or overcomplicate solutions. another point to make is that when someone says “static typing”, they often mean “static analysis” which can prevent other classes of bugs besides type errors.

Jivago Alves19:04:05

> it is often more verbose I’d argue it depends on the type inference of the language. For instance, Haskell vs Java.


Rust with its borrow checker makes race conditions go away. Not related to type system, but compiler can become quite powerful and catch a lot of errors. But arguably the "fun" of coding goes out the window as half the time you just try to make the compiler happy.


i would say static analysis rather than compiler (although the two are usually bundled together(but not necessarily)


linters are an example of static analyzers that can catch many kinds of errors before runtime , but do not compile anything


Although a static type system won't "introduce errors", it can make certain programs illegal that would otherwise run correctly.


Making sweeping changes to a large codebase to data structures is way easier in a statically typed language. Having a compiler walk you through all the affected areas is quite nice


Hillel Wayne recently wrote about this in his newsletter, but I can’t find a link to it right now. But he was referring to this post


Kris Jenkins has a talk around the same topic.


Not being a static typist myself, I do see the value of this, but I also see that this approach requires some effort. In most codebases I’ve worked with things like an SSN would be a String, which lets you pass the complete works of Shakespeare around and the type checker is still happy.