Skip to content

Commit

Permalink
Merge pull request #728 from polyadic/discard
Browse files Browse the repository at this point in the history
  • Loading branch information
bash authored May 17, 2023
2 parents d0520b0 + 52b5459 commit b735e84
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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

0 comments on commit b735e84

Please sign in to comment.