Compze.Unit 0.6.0-beta

This is a prerelease version of Compze.Unit.
dotnet add package Compze.Unit --version 0.6.0-beta
                    
NuGet\Install-Package Compze.Unit -Version 0.6.0-beta
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Compze.Unit" Version="0.6.0-beta" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Compze.Unit" Version="0.6.0-beta" />
                    
Directory.Packages.props
<PackageReference Include="Compze.Unit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Compze.Unit --version 0.6.0-beta
                    
#r "nuget: Compze.Unit, 0.6.0-beta"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Compze.Unit@0.6.0-beta
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Compze.Unit&version=0.6.0-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Compze.Unit&version=0.6.0-beta&prerelease
                    
Install as a Cake Tool

Compze.Unit

Plugging the hole in the BCL where Unit should be.

Functional programming languages do not have void. Just Unit as the value returned by methods with no meaningful thing to return. A struct with just one value.

If C# were designed today, there's good reason to believe that void would never have been part of C#. Because void creates a gaping rift in the type system. It is not a type, so you can't use it as a generic argument, return it from a Func<T>, or store it in a variable. This forces every generic API to maintain parallel versions — one for Func<T, TResult>, one for Action<T>.

Despite many years of debate, a Unit type has still not made it into the BCL. If you want a Unit type, the current choices are to roll your own, or take a dependency on some large library that happens to include a Unit type.

This tiny library exists to change that.

It consists of just the Unit struct, with a single possible value, and some static extension methods on the static class UnitConvert to help with conversions.

Usage

Return Unit instead of declaring the method void

// Before — can't be used with Func<T, TResult> APIs
public void DoSomething(string message) 
{
    /*do something*/
}

// After — works everywhere Func<T, TResult> is expected
public Unit DoSomething(string message) => Unit.Invoke(() => 
{
    /*do something*/
});

// If your code is called in extremely tight loops and you are worried about performance
public Unit DoSomething(string message)
{
    /*do something*/
    return Unit.Value;
}

Convert between Action and Func<Unit>

Action<string> stringAction = StringTakingAction;

// Action → Func
Func<string, Unit> stringFunc1 = UnitConvert.ToFunc(StringTakingAction);
Func<string, Unit> stringFunc2 = UnitConvert.ToFunc((string it) => StringTakingAction(it));
Func<string, Unit> stringFunc3 = stringAction.ToFunc();


// Func → Action
Action<string> backToAction1 = UnitConvert.ToAction(stringFunc1);
Action<string> backToAction2 = UnitConvert.ToAction((string it) => Unit.Value);
Action<string> backToAction3 = stringFunc3.ToAction();

Convert between Func<Task> and Func<Task<Unit>>

Func<string, Task> asyncAction = StringTakingAsyncAction;

// Func<Task> → Func<Task<Unit>>
Func<string, Task<Unit>> asyncFunc1 = UnitConvert.ToAsyncFunc(StringTakingAsyncAction);
Func<string, Task<Unit>> asyncFunc2 = UnitConvert.ToAsyncFunc((string it) => StringTakingAsyncAction(it));
Func<string, Task<Unit>> asyncFunc3 = asyncAction.ToAsyncFunc();


// Func<Task<Unit>> → Func<Task>
Func<string, Task> backToAsyncAction1 = UnitConvert.ToAsyncAction(asyncFunc1);
Func<string, Task> backToAsyncAction2 = UnitConvert.ToAsyncAction((string it) => Task.FromResult(Unit.Value));
Func<string, Task> backToAsyncAction3 = asyncFunc3.ToAsyncAction();
Package Description
Compze.Underscore Code that reads the way you would describe the algorithm in words. Powered by the pipe forward operator and friends.
Compze.Contracts Design-by-contract assertions

License

Apache-2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Compze.Unit:

Package Downloads
Compze.DependencyInjection

Package Description

Compze.Underscore

Code that reads the way you would describe the algorithm in words. Powered by the pipe forward operator and friends.

Compze.Tessaging

Messaging infrastructure for the Compze framework including command, query, and event handling.

Compze.Hosting

Endpoint hosting for the Compze framework — endpoints, endpoint hosts, and the builder that capabilities such as the Tessaging and Typermedia pipelines plug into.

Compze.Must

Assertions with failure messages that do the debugging for you, a trivially easy to extend fluent API and strict by default semantics.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.6.0-beta 1,068 6/4/2026
0.5.0-beta 104 3/4/2026