Skip to content

Unit Type

Zeid Youssefzadeh edited this page Feb 24, 2025 · 3 revisions

Overview

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.

Public API Summary

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.

Usage Examples

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;
    }
}
Clone this wiki locally