From 52b5459a0ba6a14e9eea43e565412290eb8f3ceb Mon Sep 17 00:00:00 2001 From: Ruben Schmidmeister <4602612+bash@users.noreply.github.com> Date: Wed, 17 May 2023 10:50:22 +0200 Subject: [PATCH] Add Discard Co-Authored-By: Thomas Bruderer --- Funcky/Discard.cs | 19 +++++++++++++++++++ Funcky/PublicAPI.Shipped.txt | 2 ++ changelog.md | 14 ++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 Funcky/Discard.cs diff --git a/Funcky/Discard.cs b/Funcky/Discard.cs new file mode 100644 index 00000000..9899fabd --- /dev/null +++ b/Funcky/Discard.cs @@ -0,0 +1,19 @@ +namespace Funcky; + +public static class Discard +{ + /// A convenient shortcut for getting a . + /// Useful for using switch expressions purely with guards when matching on non-constant values + /// or as an alternative to if / else if / else chains. + /// + /// using static Funcky.Discard; + /// return __ switch + /// { + /// _ when user.IsFrenchAdmin() => "le sécret", + /// _ when user.IsAdmin() => "secret", + /// _ => "(redacted)", + /// }; + /// + /// The name is intentionally two underscores as to not conflict with C#'s discard syntax. + public static readonly Unit __ = Unit.Value; +} diff --git a/Funcky/PublicAPI.Shipped.txt b/Funcky/PublicAPI.Shipped.txt index 70c4ba95..919b267b 100644 --- a/Funcky/PublicAPI.Shipped.txt +++ b/Funcky/PublicAPI.Shipped.txt @@ -1,4 +1,5 @@ #nullable enable +Funcky.Discard Funcky.EitherOrBoth Funcky.EitherOrBoth Funcky.EitherOrBoth.EitherOrBoth() -> void @@ -773,3 +774,4 @@ static Funcky.Unit.operator ==(Funcky.Unit left, Funcky.Unit right) -> bool static Funcky.Unit.operator >(Funcky.Unit left, Funcky.Unit right) -> bool static Funcky.Unit.operator >=(Funcky.Unit left, Funcky.Unit right) -> bool static Funcky.Unit.Value.get -> Funcky.Unit +static readonly Funcky.Discard.__ -> Funcky.Unit diff --git a/changelog.md b/changelog.md index 357048f5..47613ca8 100644 --- a/changelog.md +++ b/changelog.md @@ -17,6 +17,20 @@ var greeting = person switch record Person(string FirstName, Option LastName); ``` +### Discard +The new `Discard.__` field provides a short-hand for `Unit.Value` to be used with `switch` expressions. + +```cs +using static Funcky.Discard; + +return __ switch +{ + _ when user.IsFrenchAdmin() => "le sécret", + _ when user.IsAdmin() => "secret", + _ => "(redacted)", +}; +``` + ### Retry with Exception We've added overloads to the `Retry` and `RetryAsync` functions that allow retrying a function as long as an exception is thrown.