API Reference ResultValueExtractionExtensions - ulfbou/Zentient.Results GitHub Wiki

πŸ“˜ API Reference: static class – ResultValueExtractionExtensions

Namespace: Zentient.Results Assembly: Zentient.Results.dll Available since: v0.1.0


πŸ“– Summary

Provides value extraction and unwrapping utilities for IResult<T> instances, enabling safe or deterministic access to the success value.

These methods simplify working with IResult<T> by offering both strict (exception-throwing) and fallback-based access patterns for fluent functional pipelines.


πŸ’‘ Design Philosophy / Rationale

ResultValueExtractionExtensions enables ergonomic and predictable value retrieval while preserving safety and clarity in control flow:

  • 🧩 Functional Intent: Keeps result handling declarative and side-effect free.
  • πŸ›‘ Fail-Fast Option: Unwrap() enforces success conditions explicitly.
  • βœ… Graceful Fallbacks: GetValueOrDefault(...) supports conservative workflows where fallback behavior is expected.
  • πŸ“¦ Immutability & Consistency: Values are never mutated; methods operate over the final state of the result.

πŸ”– Type Signature

public static class ResultValueExtractionExtensions

πŸ”§ Methods

1. Unwrap<TValue>()

  • Signature:

    public static TValue Unwrap<TValue>(this IResult<TValue> result)
  • Summary: Returns the successful value, or throws if the result is a failure.

  • Parameters:

    • result (this IResult<TValue>): The result instance to unwrap.
  • Return Value: (TValue) – The success value if present.

  • Behavior: Throws InvalidOperationException if the result is a failure.

  • Exceptions Thrown:

    • ArgumentNullException (if result is null)
    • InvalidOperationException (if result is failure)
  • Remarks: Should only be used when the caller is confident the result is successful (e.g., after IsSuccess check).

  • Example Usage:

    var value = result.Unwrap(); // safe only if result.IsSuccess

2. GetValueOrDefault<TValue>(...)

  • Signature:

    public static TValue GetValueOrDefault<TValue>(this IResult<TValue> result, TValue defaultValue)
  • Summary: Returns the success value, or the specified default if the result is a failure.

  • Parameters:

    • result (this IResult<TValue>): The result instance to inspect.
    • defaultValue (TValue): The fallback value to return if the result failed.
  • Return Value: (TValue) – Either the result value or the fallback.

  • Behavior: Does not throw. Returns result.Value! on success, or defaultValue otherwise.

  • Exceptions Thrown:

    • ArgumentNullException (if result is null)
  • Remarks: Ideal for lenient pipelines or UI binding scenarios.

  • Example Usage:

    var value = result.GetValueOrDefault("fallback");

πŸ§ͺ Usage Examples

IResult<string> configResult = LoadConfig().Try();

string configValue = configResult.GetValueOrDefault("default.json");

if (configResult.IsSuccess)
{
    string actual = configResult.Unwrap(); // Guaranteed safe here
}

⚠️ Remarks

  • Unwrap() should be used with careβ€”preferably in guarded logic where IsSuccess has been verified.
  • GetValueOrDefault() is preferred for defensive and non-throwing patterns.
  • Both methods enforce the principle of functional immutabilityβ€”no internal state is modified.

🧩 Integration and Interoperability

Layer Role
Fluent Pipelines Provides deterministic access to values in chained Map/Bind logic.
UI Bindings Used for safe fallback rendering of failed result values.
Testing Utilities Simplifies assertions and mocking result outcomes.

πŸ“š See Also

  • IResult<T>
  • Result.Try(...)
  • ResultTransformationExtensions

🏷️ Tags

#API #ValueExtraction #FunctionalAPI #ExtensionMethods #ResultPattern #ZentientCore


Last Updated: 2025-06-22 Version: 0.4.0

⚠️ **GitHub.com Fallback** ⚠️