5 Understanding Static Typing

 

Types, in the terms of the working programmer, occupy an ecological niche adjacent to tests. They can fill a role similar to the tests used in Test-Driven Development. Types can provide guidance in writing code, quick feedback about errors, and documentation of APIs. Types also have interesting properties in their own right and that’s what this chapter is about. Here you’ll learn about what types are, how to solve problems with them, and finally a little about their workings.

Types are a lightweight mechanism for ruling out certain types of program behavior. Depending on how we delpoy this capability, we can look for bugs, enforce constraints, and tighten up security. Types model a simplified version of our program and examine it for flaws. Lessons learned from this simplified model carry over to the full-fledged program, allowing us to conclude things about it as well.

This chapter serves as a field guide for the rest of this book, which assumes some comfort with types. In other chapters concepts involving types are supporting characters — not the main focus, but it helps to understand them. I won’t and can’t tell you everything there is to know about types. But I want to tell you enough so that types add to rather than distract from other topics. This book is about the future of programming for the web and I think types are a part of that future.

5.1  Collections of Values

5.2  What types can be used for

5.2.1  Avoiding null-pointer errors

5.2.2  Solving injections

5.3  How types work

5.3.1  A definition of "type system"

5.3.2  How a type system is implemented

5.3.3  An example of a hard-to-spot bug

5.4  Summary