HeadlinesBriefing favicon HeadlinesBriefing.com

Why Async Programming Mirrors Dependency Injection

Hacker News •
×

Will Richardson explores how async programming fundamentally resembles dependency injection through the lens of function calls and control flow. He begins by examining how functions are abstractions that don't exist at the CPU level, with compilers handling the complexity of tracking return addresses through invisible arguments.

Richardson then traces the evolution from static dispatch in C to dynamic dispatch in Java, where method calls depend on runtime type information. He shows how closures represent a natural extension of this pattern, allowing code blocks to be passed around and executed at call sites. This leads to his central insight: both async programming and dependency injection involve jumping to arbitrary code locations determined at runtime.

The article culminates in a comparison of error handling patterns, from Go's explicit error returns to more sophisticated effect systems in languages like Koka and Effekt. Richardson argues that these approaches all share the same fundamental principle - controlling where and when code executes based on context rather than static definitions.