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

Option<TCollection>.ValueOrEmpty() #147

Open
RyanMarcotte opened this issue Sep 20, 2024 · 1 comment
Open

Option<TCollection>.ValueOrEmpty() #147

RyanMarcotte opened this issue Sep 20, 2024 · 1 comment
Milestone

Comments

@RyanMarcotte
Copy link
Collaborator

RyanMarcotte commented Sep 20, 2024

I don't generate Option<TCollection> very often, but when it happens I almost always end up transforming the Option.None case to an empty collection.

public static IEnumerable<T> ValueOrEmpty<T>(this Option<IEnumerable<T>> option)
{
    return option.ValueOrDefault(Enumerable.Empty<T>());
}

public static IReadOnlyCollection<T> ValueOrEmpty<T>(this Option<IReadOnlyCollection<T>> option)
{
    return option.ValueOrDefault(Array.Empty<T>());
}

public static T[] ValueOrEmpty<T>(this Option<T[]> option)
{
    return option.ValueOrDefault(Array.Empty<T>());
}
@JohannesMoersch JohannesMoersch added this to the Functional 3 milestone Nov 7, 2024
@JohannesMoersch
Copy link
Owner

JohannesMoersch commented Nov 7, 2024

I like this. I've added it to the Function 3 milestone. I don't remember. Are you one .NET Core or stuck on Framework?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants