From 93c1c0759c49717ccbeb9508ba0eda9f78773069 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 | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/readme.md b/readme.md index 4c73082..7fa8ef2 100644 --- a/readme.md +++ b/readme.md @@ -130,4 +130,38 @@ 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