Table of Contents

Class NullableExtensions

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Extension methods for converting nullable values to Result types. Provides a convenient way to handle null checking with Railway Oriented Programming.

public static class NullableExtensions
Inheritance
NullableExtensions
Inherited Members

Examples

// Convert nullable value type to Result
int? maybeAge = GetAge();
var result = maybeAge.ToResult(Error.Validation("Age is required"));

// Convert nullable reference type to Result
User? maybeUser = FindUser(id);
var userResult = maybeUser.ToResult(Error.NotFound("User not found", id));

// Chain with other Result operations
var validatedResult = GetUser(id)
    .ToResult(Error.NotFound("User not found"))
    .Ensure(u => u.IsActive, Error.Domain("User is inactive"));

Remarks

These extensions allow you to convert potentially null values (both nullable value types and reference types) into Result types, making null handling explicit and composable with other Result operations.

Methods

ToResult<T>(in T?, Error)

Converts a nullable value type to a Result.

public static Result<T> ToResult<T>(this in T? nullable, Error error) where T : struct

Parameters

nullable T?

The nullable value to convert.

error Error

The error to return if the value is null.

Returns

Result<T>

A success Result containing the value if not null; otherwise a failure Result with the specified error.

Type Parameters

T

The underlying value type.

ToResult<T>(T?, Error)

Converts a nullable reference type to a Result.

public static Result<T> ToResult<T>(this T? obj, Error error) where T : class

Parameters

obj T

The potentially null object to convert.

error Error

The error to return if the object is null.

Returns

Result<T>

A success Result containing the object if not null; otherwise a failure Result with the specified error.

Type Parameters

T

The reference type.