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 a possibility to retrieve the Maybe's value where a default value is returned for the none-Case #65

Closed
marco-bertschi opened this issue Dec 12, 2019 · 3 comments · Fixed by #66
Milestone

Comments

@marco-bertschi
Copy link
Contributor

marco-bertschi commented Dec 12, 2019

I'm thinking about the case where a Maybe<T> is used as a method parameter and a default value is known for the behavior in this method, for example:

public void Foo(Maybe<string> maybeBar = default)
{
   string defaultBar = "bar";
   maybeBar.Do(bar => defaultBar = bar);
}

This could be made a lot simpler with the following method wich should be added:

public void Foo(Maybe<string> maybeBar = default)
{
   // if 'bar' has a value, the value is returned, else 'defaultBar' is set to "bar"
   string defaultBar = maybeBar.ValueOrDefault("bar"); 
}
@marco-bertschi
Copy link
Contributor Author

marco-bertschi commented Dec 12, 2019

This would also allow us to have readonly properties set by a Maybe:

public class XmlTestOutputCompareAssertParameter
{
        public XmlTestOutputCompareAssertParameter(
            Maybe<string> aMaybeFileExtensionPattern = default)
        {
            aMaybeFileExtensionPattern.Do(aFileExtensionPattern => this.FileExtensionPattern = aFileExtensionPattern);
        }

        // Bad example: needs a private setter 
        public string FileExtensionPattern { get; private set; } = ".txt";
}
public class XmlTestOutputCompareAssertParameter
{
        public XmlTestOutputCompareAssertParameter(
            Maybe<string> aMaybeFileExtensionPattern = default)
        {
            this.FileExtensionPattern = aMaybeFileExtensionPattern.ValueOrDefault(".txt");
        }

        // Good example: Private setter isn't needed
        // Bonus: Default value and initialization via c'tor are together in one place.
        public string FileExtensionPattern { get; }
}

Overall, code would be cleaner as the Maybe-Pattern wouldn't enforce readonly properties to become properties with private setters when initializable via c'tor.

@marco-bertschi
Copy link
Contributor Author

@stefan-lindegger Opinions?

@marco-bertschi
Copy link
Contributor Author

marco-bertschi commented Dec 13, 2019

offline discussion with @stefan-lindegger:

  • We agree on the proposed feature
  • Additionally, ThrowIfNone should be renamed to Value to ensure a consistent interface. Its behavior shouldn't change, though.

We also would like this to be a task for someone within BBT who hasn't had the chance to gather experience with GitHub yet to get their feet wet.

stefan-lindegger pushed a commit to stefan-lindegger/BBT.Maybe that referenced this issue Jan 7, 2020
stefan-lindegger pushed a commit to stefan-lindegger/BBT.Maybe that referenced this issue Jan 7, 2020
stefan-lindegger added a commit that referenced this issue Jan 7, 2020
(GH-65) ValueOrDefault method added. ThrowExceptionIfNone method renamed.
@pascalberger pascalberger added this to the 3.0.0 milestone Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants