The Elegant Programming Language Features That Make Code Safer
A Few Good Ideas in Programming Languages
Pranoy Dutta highlights three programming language features he loves: flow typing in Crystal and TypeScript, which narrows types at compile time to feel dynamic; Rust's borrow checker, which statically prevents data races without a garbage collector; and D's contract programming, which lets you express preconditions, postconditions, and invariants directly in code. Each feature trades added complexity for stronger correctness guarantees.
I love the borrow checker because it is such an elegant solution to the problem of data races and are a zero-cost abstraction which only comes at the cost of compile-time checks and added complexity.
- vallerie
I know it's controversial but I really do love the C++26 implementing contract assertions.
I find they enable you, your consumers, IDEs, agents, etc understand the contracts of a method far faster, as it means you don't actually have to read the full method body. If the pre condition is correct and the post condition fails then you can be fairly sure the bug report goes to whoever owns that method, as either the precondition is wrong or the method is wrong.
- phtrivier
For pedantry, should we note that design by contract came all the way from Eiffel ?
(But it's possible that even less people ever wrote Eiffel than D, so, who knows)
- prydt
I didn't expect this to get posted here. Long time lurker here.
I'm really interested in programming language design and ergonomics. What niche PL features would you like to see have more adoption?
- malephex
Borrow checking (as frustrating as it is) is a good idea. I never knew about invariants in D, and now I want them in my language 's classes!
But flow typing? That seems like a footgun...
- spankalee
Nice list. I have a new language I'm working on (called Zena: https://zena-lang.dev/) with all of these in some form:
If you have static types and unions, control-flow analysis and narrowing is critical for avoiding an excessive amount of casts - and if you also have pattern matching, you get very nice style where a type-check, state extraction, and branch are all one expression.
Borrow checking. Zena is a GC'ed language, but it runs in Wasm and lots of Wasm resources are external, so Zena has affine types and second-class values for managing resources and disposing of them when no longer used. GC + borrowing is a great combo because you don't need borrowing for everything and lexical lifetimes with a few escape hatches cover most things. The ownership system is also great for modeling structured concurrency.
I'm working on contracts after borrow checking is complete. My impetus there is AI-generated code. If humans still review at all, reviewing the contacts more than the implementations makes managing large amounts of changes easier.
I'd like to see a few more good ideas spread:
Formal verification. Contracts should be a good stepping stone into a spec language, from there a proof language and checker. This should also be good for AI-generated code.
Numeric unit types / units of measure with dimensional analysis. We should be able to say that a variable isn't just a f64, but a f64 of meters, and when divided by seconds, give a velocity. I don't know why this hasn't made it […]