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
nullableT?The nullable value to convert.
errorErrorThe 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
TThe 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
objTThe potentially null object to convert.
errorErrorThe 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
TThe reference type.