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

Allow access to Exception member #47

Open
avilv opened this issue Dec 11, 2017 · 1 comment
Open

Allow access to Exception member #47

avilv opened this issue Dec 11, 2017 · 1 comment

Comments

@avilv
Copy link

avilv commented Dec 11, 2017

There are certain use cases that require access to that member

just as the Unsafe namespace allows access to Value, same should be with Exception

@nlkl
Copy link
Owner

nlkl commented Dec 17, 2017

Hi,

Thank you for your interest in the project.

I understand the concern - currently the ways in which you can access the exceptional value are rather limited, which is something I will certainly look into improving in a future release.

For now, I suggest you simply introduce your own set of extension methods to suit your needs, e.g. something like:

public static class OptionExceptionExtensions
{
    public static TException ExceptionOr<TValue, TException>(this Option<TValue, TException> option, Func<TException> alternativeFactory) =>
        option.Match(_ => alternativeFactory(), exception => exception);

    public static TException ExceptionOr<TValue, TException>(this Option<TValue, TException> option, Func<TValue, TException> alternativeFactory) =>
        option.Match(alternativeFactory, exception => exception);
}

// And use it like:
option.ExceptionOr(() => throw new InvalidOperationException("Unexpected value"));
option.ExceptionOr(value => throw new InvalidOperationException($"Unexpected value: {value}");

I will consider adding similar functionality in a future release, but introducing your own set of additional utility methods is probably the easiest way to get the functionality today.

Hope it helps.

Best regards,
/ Nils

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