Richard Waller

On Type Safety

Type systems are often discussed in terms of preventing errors—catching bugs before they reach production. But this framing misses something deeper: types are a form of communication, a way of encoding intent that both humans and machines can understand.

When I designed Neon, the goal wasn’t just to catch SQL injection or type mismatches. It was to make queries self-documenting, to create a language where invalid operations were impossible to express.

The Promise of Types

Consider a traditional ORM query:

users = User.query.filter(User.email == email).all()  

This looks safe, but what if email is None? What if the database schema changed and email is now email_address? What if the query returns millions of rows and you meant to use .first()?

These errors surface at runtime. In a large codebase, they’re easy to miss. A test might pass, production might work for months, then suddenly fail when an edge case is hit.

With Neon’s type-safe queries:

usersByName :: Text -> Query [User]  
usersByName search = select $ do  
  user <- from table  
  where_ (user.email `like` ("%" <> search <> "%"))  
  return user  

The type system enforces:

  • search must be Text, not Maybe Text or Int
  • The result is [User], not Maybe User or User
  • user.email exists and is the right type
  • The query is valid SQL

If the schema changes, the query fails to compile. If you use the wrong type, it fails to compile. The type system becomes a specification, checked automatically.

Types as Documentation

But types do more than prevent errors—they document intent. When you see:

fn process_packet(&mut self, mbuf: &mut Mbuf) -> Result<(), Error>  

You immediately know:

  • The function takes a mutable reference (it will modify state)
  • It returns a Result (it can fail)
  • The error type is explicit (you know what can go wrong)

Compare this to:

int process_packet(void* mbuf);  

What does this return? Zero for success? Negative for error? What errors are possible? Does it modify mbuf? The type system doesn’t tell you.

In Prism, we use Rust’s type system extensively. The borrow checker prevents data races. The type system ensures zero-copy operations are actually zero-copy. The Result type makes error handling explicit.

The Cost of Types

Type systems aren’t free. They require:

  • More upfront thinking about design
  • More verbose code in some cases
  • Learning curve for developers
  • Compilation time (though this is often overstated)

But these costs are often overstated. Modern type systems—Haskell, Rust, TypeScript—have excellent type inference. You write less boilerplate than you might expect. And compilation time, while real, is often offset by catching errors early.

The real question isn’t whether types have costs—it’s whether the benefits outweigh them. For systems where correctness matters—databases, network stacks, compilers—the answer is usually yes.

Gradual Typing

Not every system needs a full type system from day one. Gradual typing—adding types incrementally—can be a good middle ground. TypeScript allows JavaScript code to coexist with typed code. Python’s type hints are optional but useful.

The key is starting with types where they matter most: public APIs, data structures, error paths. Then gradually expanding coverage.

Conclusion

Type safety isn’t just about preventing bugs—it’s about creating better abstractions. When types encode your intent, the compiler becomes a partner in correctness. When types document behavior, code becomes self-explanatory.

This is why Neon compiles queries at build time, why Prism uses Rust’s type system for memory safety, why Quasar uses strong typing for its state machine. Not because types prevent all errors, but because they make correct code easier to write and incorrect code impossible to express.

References



Links to this page

Change Log
Home