diff --git a/Readme.md b/Readme.md index 6389248..547ac7d 100644 --- a/Readme.md +++ b/Readme.md @@ -340,9 +340,42 @@ public static async Task CreateUserAsync( } ``` +## Unwrapping + +To bypass exhaustive matching and access a variant directly, use the variant-specific `Unwrap` methods. + +This can be useful if you're sure of the underlying value or if you don't care about a potential exception at runtime. + +```cs +using Dunet; + +[Union] +partial record Option +{ + partial record Some(T Value); + partial record None; +} +``` + +```cs +Option option1 = new Option.Some(3.14); +var some = option.UnwrapSome(); +// You can access `Value` directly here. +Console.WriteLine(some.Value); // Prints "3.14". + +Option option2 = new Option.None(); +// Throws `InvalidOperationException` because the underlying variant is `None`. +var bad = option.UnwrapSome(); +``` + +> **Note**: +> Unwrapping is unsafe. Use only when runtime errors are ok. + ## Stateful Matching -To reduce memory allocations, use the `Match` overload that accepts a generic state parameter as its first argument. This allows your match parameter lambdas to be `static` but still flow state through: +To reduce memory allocations, use the `Match` overload that accepts a generic state parameter as its first argument. + +This allows your match parameter lambdas to be `static` but still flow state through: ```cs using Dunet; diff --git a/src/Dunet.csproj b/src/Dunet.csproj index 8347119..27c4ca4 100644 --- a/src/Dunet.csproj +++ b/src/Dunet.csproj @@ -14,14 +14,14 @@ Readme.md https://github.com/domn1995/dunet source; generator; discriminated; union; functional; tagged; - 1.9.0 - 1.9.0 + 1.10.0 + 1.10.0 MIT https://github.com/domn1995/dunet/releases git favicon.png False - 1.9.0 + 1.10.0 en true $(NoWarn);NU5128