C# currently does not natively support discriminated unions as a language feature, but it has been on the roadmap now for several months (years?). The

C# Discriminated Unions and .NET Channels

submited by
Style Pass
2024-07-10 11:30:05

C# currently does not natively support discriminated unions as a language feature, but it has been on the roadmap now for several months (years?).

There are a lot of benefits and use cases for DUs, but one of my favorites is using them in conjunction with .NET channels because it is common that there is a need to process a single record with different logic and then combine the results at the end.

This is fine if each step is fast and cannot be parallelized. But what if each step is costly and actually discrete (no dependency on the previous result)? Processing all three steps in parallel would make better use of hardware and improve the throughput of the system.

Imagine that the design is for a system that is processing a CSV file that has a list of call logs and the processing pipeline needs to run a series of prompts over the call record in parallel to:

Since these actions are all discrete, each can be processed in parallel (e.g. using different LLM prompts) and then each facet of the call log can simply be aggregated at the end into a single record to be routed.

Leave a Comment