Hyperbee Expressions
Hyperbee.Expressions extends the .NET expression tree model with language constructs that the standard System.Linq.Expressions library does not support: asynchronous workflows, enumerable state machines, structured loops, resource disposal, string formatting, and dependency injection.
All custom expression types reduce to standard expression trees, so they work with any compiler that accepts LambdaExpression – including the System compiler, FastExpressionCompiler, and the Hyperbee Expression Compiler, which these expressions are designed to pair with.
Packages
The expression compiler is built and documented in its own repository, hyperbee.expressions.compiler.
Getting Started
dotnet add package Hyperbee.Expressions
All factory methods live in ExpressionExtensions. Import them with:
using static Hyperbee.Expressions.ExpressionExtensions;
Quick Example
using static System.Linq.Expressions.Expression;
using static Hyperbee.Expressions.ExpressionExtensions;
// Build an async expression that awaits two tasks sequentially
var result1 = Variable( typeof(int), "result1" );
var result2 = Variable( typeof(int), "result2" );
var expr = BlockAsync(
[result1, result2],
Assign( result1, Await( Call( typeof(MyService).GetMethod("GetValueAsync") ) ) ),
Assign( result2, Await( Call( typeof(MyService).GetMethod("GetOtherAsync"), result1 ) ) ),
result2
);
var lambda = Lambda<Func<Task<int>>>( expr );
var fn = lambda.Compile();
var value = await fn();
Expression Types
Async
| Type | Factory Method | Description |
|---|---|---|
AsyncBlockExpression | BlockAsync(...) | Async code block with generated state machine |
AwaitExpression | Await(...) | Await a task or awaitable |
Enumerable / Yield
| Type | Factory Method | Description |
|---|---|---|
EnumerableBlockExpression | BlockEnumerable(...) | Enumerable block with generated state machine |
YieldExpression | YieldReturn(...) / YieldBreak() | Yield a value or break from enumeration |
Loops
| Type | Factory Method | Description |
|---|---|---|
ForExpression | For(...) | for loop with init / test / iteration |
ForEachExpression | ForEach(...) | foreach loop over any IEnumerable |
WhileExpression | While(...) | while loop |
Resource Management
| Type | Factory Method | Description |
|---|---|---|
UsingExpression | Using(...) | using block for IDisposable |
Utilities
| Type | Factory Method | Description |
|---|---|---|
StringFormatExpression | StringFormat(...) | string.Format in an expression |
DebugExpression | Debug(...) | Debug callback for expression trees |
InjectExpression | Inject(...) | Resolve a service from IServiceProvider |
ConfigurationExpression | ConfigurationValue(...) | Read a value from IConfiguration |
Compiler
These expressions are designed to pair with the Hyperbee Expression Compiler (HEC), an IR-based compiler that emits IL directly in place of Expression.Compile().
Speed is the reason. HEC compiles 9.3-48x faster than the System compiler and allocates 29-57% less. Coroutines gain most: BlockAsync and BlockEnumerable compile about twice as fast and the compiled state machines run 16-24x faster, because HEC emits the MoveNext body into the state machine type rather than leaving a delegate to invoke on each resume.
It is optional. HEC plugs in through the contracts in Hyperbee.Expressions.Abstractions, so nothing here depends on it.
dotnet add package Hyperbee.Expressions.Compiler
Its documentation, benchmarks and architecture live in its own repository.
Lab
Hyperbee.Expressions.Lab provides experimental expression types for HTTP fetch, JSON parsing, and collection map/reduce operations.
dotnet add package Hyperbee.Expressions.Lab
See Lab for details.
Credits
Special thanks to:
- Sergey Tepliakov – Dissecting the async methods in C#
- Fast Expression Compiler for improved performance
- Just The Docs for the documentation theme
Contributing
We welcome contributions! Please see our Contributing Guide for more details.