2 ms·
The type system is a unit test of expected inputs and outputs - it ensures that values are bounded to their domains, and aren't manipulated incorrectly. The ad
by barrkel 17y ago
The type system is a unit test of expected inputs and outputs - it ensures that values are bounded to their domains, and aren't manipulated incorrectly.
The advantage of doing it like Ayende says is when you already have a mental model of the transformation that you want to perform, the compiler can do the work of showing you the bits you need to edit to finalize the transformation. This process isn't mindless; because you've informed the type system about the substructure of your code, when you want to change the shape of that substructure, the compiler is then able to tell you exactly where it is now inconsistent.
Whereas with dynamically typed code, you need unit tests with 100% path coverage (line coverage isn't enough) to guarantee that you caught every interaction, if you were to approach the change in the same way. (As an aside: that's a whole lot of tests that need rewriting and refactoring, one reason I don't like unit testing for very early stages of a project when the design is quite fluid.) The alternative is eyeballing the code and perhaps getting a runtime type violation or two before all the locations are gotten right.
Note that here I'm only talking about finding the locations to modify, not about guaranteeing that the modifications are correct. If the structural change is simple, where all the in-place modifications are trivial, having a static type system that tells you about all the locations is really handy, whereas you need to do a lot more work with a dynamic type system.
IMO the benefits of a dynamic type system are more apparent when the systems you're working with are themselves dynamic and (relatively) quickly changing in nature, or when the substructure is too tedious to efficiently express in a static type system (especially apparent with mutually recursive generic types with dependencies among type parameters, like you might find in generic parsing and graph transformation libraries, etc.).