TartanLlama / expected

C++11/14/17 std::expected with functional-style extensions
https://tl.tartanllama.xyz
Creative Commons Zero v1.0 Universal
1.55k stars 135 forks source link

"Pipe" operator for chaining operations #151

Closed firedaemon-cto closed 1 year ago

firedaemon-cto commented 1 year ago

I am not sure if you have considered this possibility: I find it much more generic to provide "free functions" to decouple the extension of functionality from the core of a class than the intrusive way via member methods. This way, the standard std::epected doesn't have to be replaced or updated by a proposal, and its use can be made even more useful.

The "pipe" operator|() can be used as a convenient way to concatenate things, and the syntax looks and feels as natural as the "dot" operator:

tl::expected<image,fail_reason> get_cute_cat (const image& img) {
    return crop_to_cat(img)
           | and_then(add_bow_tie)
           | and_then(make_eyes_sparkle)
           | map(make_smaller)
           | map(add_rainbow);
}

This would also apply to your proposal for monadic operations for std::optional.

firedaemon-cto commented 1 year ago

Oh, I see now that free functions were taken into consideration.