From 9fcf7ca2f716c21dda0987202b83e720f3115ca6 Mon Sep 17 00:00:00 2001 From: Mathias Fischler Date: Tue, 1 Oct 2024 11:10:09 +0200 Subject: [PATCH] Update readme with example for partition --- readme.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/readme.md b/readme.md index 4c73082..4294fa6 100644 --- a/readme.md +++ b/readme.md @@ -130,4 +130,40 @@ partial record Shape +### `GeneratePartitionExtension` +Set `GeneratePartitionExtension` to true to auto-generate partition methods for `IEnumerable` of your type. + +```cs +using Funcky; +using System.Text.Serialization; + +[DiscriminatedUnion(GeneratePartitionExtension = true)] +public abstract partial record Result +{ + public sealed partial record Ok() : Result; + + public sealed partial record Warning(string Message) : Result; + + public sealed partial record Error(string Message) : Result; +} +``` + +
+ +Usage + +```cs +var results = new Result[] { new Result.Ok(), /* ... */ } + +// N-Tuple extension method: +var (oks, warnings, errors) = results.Partition(); + +// Extension method with result selector: +var warningAndErrorCount = results.Partition(resultSelector: (_, warnings, errors) => warnings.Count + errors.Count); +``` + +
+ + + [json-polymorphism-docs]: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism