π― The Problem That Started My Journey
I was diving deep into functional programming and Result patterns in C#, but I kept hitting the same wall: type information loss in fluent APIs.
// The problem I kept facing
Reason reason = new Error("Something went wrong")
.WithMessage("Updated message") // Returns Reason, not Error
.WithTag("Code", 404); // Can't call Error-specific methods
π‘ Discovering CRTP: The "Aha!" Moment
That's when I discovered the Curiously Recurring Template Pattern (CRTP). At first, it looked like magic:
// CRTP in action
public abstract class Reason<TReason> : Reason
where TReason : Reason<TReason>
{
public TReason WithMessage(string message)
{
Message = message;
return (TReason)this; // Returns derived type!
}
}
Suddenly, everything clicked. The compiler could preserve type information through the entire fluent chain!
π§ Building REslava.Result
With CRTP as my foundation, I started building REslava.Result - a zero-dependency Result pattern library that solves the type preservation problem.
// Perfect type preservation with CRTP
Error error = new Error("Something went wrong")
.WithMessage("Updated message") // Returns Error
.WithTag("Code", 404) // Still Error - can chain more
.WithTag("Field", "Email"); // Perfect fluent API
This isn't just about pretty code - it's about compile-time safety and developer experience.
π What I'm Learning Along the Way
Building this library has been an incredible learning journey:
- Advanced C# patterns - CRTP, generics, covariance
- Functional programming - Result patterns, monads, railway-oriented programming
- Library design - API design, backwards compatibility, zero dependencies
- Production thinking - Performance, testing, documentation
Every day brings new challenges and discoveries about what's possible in C#.
π― Why This Matters
Most Result libraries don't solve the fluent API type preservation problem. They either:
- Accept the type loss (limiting)
- Use complex workarounds (overly complicated)
- Ignore fluent APIs altogether (missing opportunity)
CRTP provides an elegant solution that's both powerful and intuitive.
π Come Learn With Me
I'm documenting this entire journey as I build REslava.Result:
I'd love to hear from others who have explored CRTP or are interested in functional programming in C#. What challenges have you faced? What solutions have you discovered?
π Tags
csharp dotnet functionalprogramming crtp librarydevelopment resultpattern fluentapi
Top comments (0)