Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discard #728

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Funcky/Discard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Funcky;

public static class Discard
{
/// <summary>A convenient shortcut for getting a <see cref="Unit"/>.
/// Useful for using switch expressions purely with guards when matching on non-constant values
/// or as an alternative to <c>if</c> / <c>else if</c> / <c>else</c> chains.</summary>
/// <example><code>
/// using static Funcky.Discard;
/// return __ switch
/// {
/// _ when user.IsFrenchAdmin() => "le sécret",
/// _ when user.IsAdmin() => "secret",
/// _ => "(redacted)",
/// };
/// </code></example>
/// <remarks>The name is intentionally two underscores as to not conflict with C#'s discard syntax.</remarks>
public static readonly Unit __ = Unit.Value;
}
2 changes: 2 additions & 0 deletions Funcky/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Funcky.Discard
Funcky.EitherOrBoth
Funcky.EitherOrBoth<TLeft, TRight>
Funcky.EitherOrBoth<TLeft, TRight>.EitherOrBoth() -> void
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ var greeting = person switch
record Person(string FirstName, Option<string> 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.
Expand Down