dotResult 2.0.0-alpha.2

This is a prerelease version of dotResult.
There is a newer version of this package available.
See the version list below for details.
dotnet add package dotResult --version 2.0.0-alpha.2
                    
NuGet\Install-Package dotResult -Version 2.0.0-alpha.2
                    
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="dotResult" Version="2.0.0-alpha.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="dotResult" Version="2.0.0-alpha.2" />
                    
Directory.Packages.props
<PackageReference Include="dotResult" />
                    
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 dotResult --version 2.0.0-alpha.2
                    
#r "nuget: dotResult, 2.0.0-alpha.2"
                    
#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 dotResult@2.0.0-alpha.2
                    
#: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=dotResult&version=2.0.0-alpha.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=dotResult&version=2.0.0-alpha.2&prerelease
                    
Install as a Cake Tool

dotResult

CI

A Result<T, TError> for C# 15 built on union types: two explicit cases, exhaustive pattern matching, and no hidden exceptions.

Status: 2.0 is a ground-up rewrite for .NET 11 and C# 15 unions, under active development. The 1.x line is retired.

using DotResult;
using static DotResult.Prelude;

Result<int, string> Parse(string text) =>
    int.TryParse(text, out var number) ? ok(number) : error("not a number");

var message = Parse("42") switch
{
    Ok<int>(var value) => $"parsed {value}",
    Error<string>(var error) => error,
};
  • Union types. Result<T, TError> has exactly two cases, Ok<T> and Error<TError>. Both convert implicitly, and a switch that handles both is exhaustive. Result<string, string> is never ambiguous.
  • No invalid values. null is never a value or an error, and there is no .Value, IsOk or other accessor that can throw: you get a value out through pattern matching or total functions.
  • No allocations. For value types, creating, matching and comparing results allocates nothing.
  • Functional API. Match, OrDefault, Map, MapError, Bind and Ensure today; query syntax, async pipelines, combinators, collections and JSON arrive through the 2.0.0 alphas.

Usage

Import the namespace, and Prelude for the lower-case ok and error:

using DotResult;
using static DotResult.Prelude;

Creating

Result<int, string> answer = ok(42);              // a success
Result<int, string> failure = error("no answer"); // an error
Result<int, string> parsed = int.TryParse(text, out var number) ? ok(number) : error("not a number");

ok and error return the cases, Ok<int> and Error<string>, which convert to a result with any error or value type. A bare value does not convert: Result<int, string> answer = 42; does not compile, so a result always says which case it is, even when both types are the same (Result<string, string>).

The functions are lower case on purpose, so they never clash with Ok or Error types of your own. null is never a value or an error: ok(null!) and error(null!) throw ArgumentNullException, and nullable analysis warns about a possibly-null argument first.

Getting values out

var text = answer switch
{
    Ok<int>(var value) => $"The answer is {value}",
    Error<string>(var error) => $"No answer: {error}",
};

if (parsed is Ok<int>(var number))
{
    Console.WriteLine(number);
}

var same = answer.Match(value => $"The answer is {value}", error => $"No answer: {error}");
var orZero = answer.OrDefault(0);
var fromError = answer.OrDefault(error => error.Length);   // the fallback sees the error

Both cases make the switch exhaustive, and the compiler warns when one is missing. Pattern matching works in generic code too: result is Ok<T>(var value) reads the value when T is a type parameter. Match runs exactly one branch, exactly once; the OrDefault factory runs only for an error.

Transforming and chaining

Result<int, string> Half(int value) => value % 2 == 0 ? ok(value / 2) : error("odd");

var doubled = answer.Map(value => value * 2);               // Ok(84); an error passes through
var halved = answer.Bind(Half);                             // a step that can fail; the first error wins
var detailed = answer.MapError(error => $"lookup failed: {error}"); // translate the error, keep the value
var small = answer.Ensure(value => value < 100, "too large"); // a failed check becomes the error
var adult = answer.Ensure(value => value >= 18, value => $"{value} is under 18"); // the error can use the value

Map and MapError change one side and leave the other alone; Bind continues a success with a step that returns a result with the same error type (line error types up with MapError first); Ensure turns a success that fails a check into an error. A step runs only for a success, exactly once.

Union conversions take no part in type inference, so a Bind lambda that returns ok(...) or error(...) needs the value type spelled out:

var positive = answer.Bind<int>(value => value > 0 ? ok(value) : error("not positive"));

A step that already returns a result (answer.Bind(Half)) needs nothing. A null from Map or MapError is rejected with InvalidOperationException: a result never holds null.

The uninitialized result

Result<T, TError> is a struct, so default(Result<T, TError>) exists, for example in a new array. It is neither case: a switch over both cases throws SwitchExpressionException, and every operation that reads it throws InvalidOperationException. Equality, hashing and ToString() (Uninitialized) still work, and result is null is true only for an uninitialized result. Always create results with ok or error.

Requirements

.NET 11 (C# 15).

Building

dotnet build dotResult.slnx
dotnet test --solution dotResult.slnx

The SDK version is pinned in global.json. Tests run on Microsoft Testing Platform.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net11.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 100 11/14/2026
2.0.0-beta.2 58 11/14/2026
2.0.0-beta.1 61 11/14/2026
2.0.0-alpha.8 58 11/14/2026
2.0.0-alpha.7 58 11/14/2026
2.0.0-alpha.6 61 11/13/2026
2.0.0-alpha.5 59 11/13/2026
2.0.0-alpha.4 60 11/13/2026
2.0.0-alpha.3 62 11/13/2026
2.0.0-alpha.2 56 11/13/2026
2.0.0-alpha.1 64 11/13/2026