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

Methods directly on containers (e.g maybe) #288

Closed
tom91136 opened this issue Dec 25, 2023 · 1 comment
Closed

Methods directly on containers (e.g maybe) #288

tom91136 opened this issue Dec 25, 2023 · 1 comment

Comments

@tom91136
Copy link
Contributor

I was wondering would adding a few more methods to some of the containers would help with productivity.
For example:

    auto o = fplus::maybe<int>(1);
    auto p = fwd::apply(
        o,
        fwd::and_then_maybe([](auto x) { return x > 0 ? fplus::just(1) : fplus::nothing<int>(); }),
        fwd::lift_maybe([](auto x) { return x * 2; }),
        fwd::just_with_default(42)
    );

    auto p2 = o
            .and_then([](auto x) { return x > 0 ? fplus::just(1) : fplus::nothing<int>(); })
            .lift([](auto x) { return x * 2; })
            .get_with_default(42);

That way:

  • It aligns with the C++23 monadic interfaces on optional
  • We can drop the _maybe suffix as it's clear based on the method receiver
  • We should still keep the free functions but those simply delegates to the instance implementation, so we don't need to
    forward declare it or do anything awkward.
  • For most cases (e.g chaining on the same functor), we can get the same effect of using fwd but without the extra boilerplate.

I'm happy to open a PR if we think this is a good idea.

@Dobiasd
Copy link
Owner

Dobiasd commented Dec 25, 2023

Oh, that looks like a splendid idea to me in terms of code readability. ❤️

Yeah, we should keep the free functions (to not break old client code). And, I agree, letting the free functions forward to the member functions (instead of the other way around) is the way to go, exactly because of the reason you mentioned. 👍

I'd be happy to receive your PR. 🙂

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