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 monadic operations (P2505) #60

Merged
merged 4 commits into from Sep 30, 2023

Conversation

szaszm
Copy link
Contributor

@szaszm szaszm commented Sep 24, 2023

  • implement std::invoke as detail::invoke
  • implement and_then, or_else, transform and transform_error (both normal and expected<void>)
  • write some tests
  • add configuration macros
  • implement and test error_or
  • update README

P2505R3 proposal: https://wg21.link/P2505R3
P2505R5 proposal: https://wg21.link/P2505R5

I tested with GCC 13.2, Clang 16, and with the GitHub Actions CI job. Tried my best to follow the formatting conventions, but might have missed a few spots.

Related to #56

Let me know if any changes are necessary. I realize that this is a large diff to review at once. I can split it up to multiple parts if necessary.

- implement std::invoke as detail::invoke
- add the monadic functions from P2505
- write some tests
I accidentally discovered that by declaring a templated data member
pointer type, the type can be deduced to a function type, resulting in a
member function pointer type. This was I could deduce cv-qualifiers and
noexcept-ness all in one, without having to resort to more
metaprogramming or writing 8x overloads.

Removed parens and added SFINAE checks to clarify that the parameter is
a member pointer type, and the member type gets deduced to a function
type, resulting in a member function pointer.
@martinmoene
Copy link
Owner

martinmoene commented Sep 26, 2023

Oh, wow, thank you very much for all this work!

Also thanks for trying and follow my idiosyncratic code layout :)

Wrote some remarks below I could come up with now.
Please indicate if you'd like to do (aspects of) these, otherwise I'll take care of them.


Initial remarks, things todo (irrespective of who will):

nsel_P2505R

Wrote "expected-lite uses 3 and higher" below based on a quick inspection, unsure if it is correct.

// Monadic operations proposal revisions:
//
// P2505R0:  0 (2021-12-12)
// P2505R1:  1 (2022-02-10)
// P2505R2:  2 (2022-04-15)
// P2505R3:  3 (2022-06-05) *
// P2505R4:  4 (2022-06-15)
// P2505R5:  5 (2022-09-20)
//
// expected-lite uses 3 and higher

#ifndef  nsel_P2505R
# define nsel_P2505R  3
#endif

// ...

#if nsel_P2505R >= 3
// P2505 code
#endif

@szaszm
Copy link
Contributor Author

szaszm commented Sep 26, 2023

I can have a look at these later this week. I'm happy that my contribution is appreciated.

My intention was to implement the C++23 draft version, which is P2505R5, except for error_or (a separate feature IMO), but it ended up closer to R3. I just noticed that I should've used remove_cv_t behavior (instead of remove_cvref_t equivalent) for transform and transform_error, I'll fix that.

I'm planning to finish this, including the docs and feature selection macro, but if you do it before me, that's also fine by me. If you want to work on this feature before I do, you have full commit/write access to the pull request source branch on my fork.

@martinmoene
Copy link
Owner

martinmoene commented Sep 26, 2023

Appreciate it if you could do (most of) it. Chances are you'll be quicker than I'll be :)

@szaszm szaszm changed the title Add monadic operations (P2505, except error_or) Add monadic operations (P2505) Sep 30, 2023
@szaszm
Copy link
Contributor Author

szaszm commented Sep 30, 2023

Added the config macro, updated to R5 by default (including an error_or implementation), but R3 can still be selected with the macro.

In the README, I updated the "Interface of expected" section, not the "Algorithms for expected", because these are only member functions in the interface section, while there were only free standing functions in the Algorithms section, and the additions are all member functions.

Let me know if this looks good, or there are any more changes needed.

@martinmoene
Copy link
Owner

Looks great to me :)
Thank you so much for this nice, substantial contribution!

@martinmoene martinmoene merged commit a314c9a into martinmoene:master Sep 30, 2023
7 checks passed
martinmoene pushed a commit that referenced this pull request Sep 30, 2023
…60, thanks @szaszm)

and conditionally compile `invoke` tests, only test if `invoke` itself
is compiled
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

Successfully merging this pull request may close these issues.

None yet

2 participants