Skip to content

Getting a value from an Option without 'Some' in value #1142

Answered by louthy
speterson-zoll asked this question in Q&A
Discussion options

You must be logged in to vote

Not sure if I'm entirely parsing what you're asking, but I think there's an alternative route that's more elegant. Really you want to work with the Some state when you know it is a Some state, not try to hack it to be either the value or an empty string after-the-fact:

public static Error InvalidRange(DateTime s, DateTime e) =>
    Error.New($"The value of the start date '{s}' cannot be greater than the end date '{e}'.");

public static Validation<Error, Unit> ValidateDateRange(
    Option<DateTime> startDateTime,
    Option<DateTime> endDateTime) =>
    (startDateTime.Case, endDateTime.Case) switch
    {
        (DateTime s, DateTime e) when s > e => InvalidRange(s, e),
        _ => Success

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@speterson-zoll
Comment options

Answer selected by speterson-zoll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants