Skip to content

Feature suggestion: Free monadic-optional functions usable with pipe syntax. #62

@BenFrantzDale

Description

@BenFrantzDale

Feel free to close if you don't like this idea, but in our C++17 (soon-to-be-20) codebase, we've added namespace MonadicOptional that has pipeable transform, and_then, etc. These are cool in that (a) they let us operate on std::optional pre-C++23, but also, they let us treat pointers the same way:

using namespace MonadicOptional;
STATIC_REQUIRE(
    (std::make_unique<int>(80) //
     | and_then([](auto x) { return std::optional{x / 4}; })
     | transform([](auto x) { return x + 1; })
     | and_then([](auto x) { return std::optional{x * 2}; }))
    == std::optional{42}
);

This feels useful even after we get C++23. Would you be interested in including functionality like this in this library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions