-
Notifications
You must be signed in to change notification settings - Fork 0
Unit Type
The Unit
type is a lightweight, immutable record struct designed to represent a value with a single, constant instance. It is analogous to void
in imperative programming but offers a concrete type that is particularly useful in functional and railway-oriented programming (ROP) scenarios. Key design patterns and benefits include:
- Immutable Design & Value Semantics: Ensures thread safety and predictable behavior by preventing state changes.
-
Railway-Oriented Programming (ROP):
Facilitates fluent chaining of operations (e.g., with
Result<T>
) for clear error handling and composition. -
Interoperability:
Implicit conversion to and from
ValueTuple
allows seamless integration with other functional constructs.
These patterns enhance developer experience by simplifying error handling, reducing boilerplate code, and offering performance benefits due to the lightweight nature of the type.
Member | return Type | Description |
---|---|---|
Default |
Unit |
The single instance representing the absence of a meaningful value. |
ToString() |
string |
Returns the string representation "()" for the Unit instance. |
Return<T>(T anything) |
T |
Transforms a Unit into a meaningful value of type T . |
Return<T>(Func<T> anything) |
T |
Lazily produces an alternative value of type T when needed. |
implicit operator ValueTuple(Unit _) |
ValueTuple |
Converts a Unit instance to an empty ValueTuple for interoperability. |
implicit operator Unit(ValueTuple _) |
Unit |
Converts an empty ValueTuple to a Unit instance. |
Below are brief examples demonstrating how to use the Unit
type:
Example: Chaining Operations with ROP
using ZeidLab.ToolBox.Common;
using ZeidLab.ToolBox.Results;
public class Example
{
private Result<Unit> LogMessage(string message)
{
// Logging logic here...
return Result.Success(Unit.Default);
}
private Result<Unit> ValidateConfiguration()
{
// Configuration validation logic...
return Result.Success(Unit.Default);
}
public void ExecuteWorkflow()
{
// Start with a successful operation using Unit
Result<Unit> result = Result.Success(Unit.Default);
// Chain operations using Bind for fluent error handling
Result<Unit> workflow = result
.Bind(_ => LogMessage("First step completed"))
.Bind(_ => ValidateConfiguration());
}
}
Example: Converting Unit to a Meaningful Value
using ZeidLab.ToolBox.Common;
public class Example
{
public void ConvertUnit()
{
// Direct conversion using a static Return method
int defaultValue = Unit.Default.Return(42);
// Lazy evaluation with a lambda expression
string alternative = Unit.Default.Return(() => "Lazy Evaluation");
}
}
Example: Implicit Conversion with ValueTuple
using ZeidLab.ToolBox.Common;
public class Example
{
public void ImplicitConversion()
{
// Implicitly convert Unit to ValueTuple
ValueTuple tuple = Unit.Default;
// Implicitly convert ValueTuple back to Unit
Unit unitFromTuple = tuple;
}
}
Inspired by LanguageExt, this library offers a more compact and user-friendly alternative with extensive examples and tutorials.
There is a very detailed YouTube channel with a dedicated video tutorial playlist for this library.
Star this repository and follow me on GitHub to stay informed about new releases and updates. Your support fuels this project's growth!
If my content adds value to your projects, consider supporting me via crypto.
- Bitcoin: bc1qlfljm9mysdtu064z5cf4yq4ddxgdfztgvghw3w
- USDT(TRC20): TJFME9tAnwdnhmqGHDDG5yCs617kyQDV39
Thank you for being part of this community—let’s build smarter, together