Skip to content

Add compose_first_error_and_result, closes #265#329

Merged
Dobiasd merged 3 commits into
masterfrom
add-compose-log-and-result
Apr 27, 2026
Merged

Add compose_first_error_and_result, closes #265#329
Dobiasd merged 3 commits into
masterfrom
add-compose-log-and-result

Conversation

@Dobiasd

@Dobiasd Dobiasd commented Apr 27, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds compose_first_error_and_result in result.hpp: runs two Result-returning functions on the same input independently.
  • The first contributes only its potential error (as Maybe); its Ok value is discarded.
  • The second's Result is forwarded unchanged, and the second is always called regardless of whether the first failed.
  • Useful for ancillary checks (logging, metrics, validation, audit, cache lookup, ...) that may fail but should not block the main computation.
// first  : a -> Result<X, E1>     (Ok value is discarded; only error is captured)
// second : a -> Result<T, E2>     (called regardless of whether first failed)
// returns: a -> std::pair<Maybe<E1>, Result<T, E2>>

Resolves the design discussed in #265.

Test plan

  • New result_test - compose_first_error_and_result test case covers all four combinations: (first ok / fail) × (second ok / fail).
  • Full test suite passes (ctest: 424/424).
  • generate/auto_generate.py ran; amalgamated include_all_in_one/include/fplus/fplus.hpp updated.

🤖 Generated with Claude Code

Pairs a possibly-failing side-effecting function (e.g. logging) with a
regular result-returning function. Both are called on the same input;
the second runs regardless of whether the first one failed. The error
of the first is preserved as a Maybe alongside the second's Result.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread test/result_test.cpp Fixed
The combinator is not specific to logging — it fits any ancillary
check that may fail but should not block the main computation
(validation, metrics, audit, cache lookup, ...). Generalize the
name and the doc comment accordingly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Dobiasd Dobiasd changed the title Add compose_log_and_result, closes #265 Add compose_first_error_and_result, closes #265 Apr 27, 2026
Comment thread test/result_test.cpp Dismissed
std::monostate requires C++17 but the project compiles as C++14, so the
test broke every CI compiler. The Ok value of the first function is
discarded by the combinator anyway, so any type works -- use int.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Dobiasd Dobiasd mentioned this pull request Apr 27, 2026
@Dobiasd Dobiasd merged commit 8e92456 into master Apr 27, 2026
49 checks passed
@Dobiasd Dobiasd deleted the add-compose-log-and-result branch April 27, 2026 06:44
@Dobiasd Dobiasd mentioned this pull request Apr 27, 2026
2 tasks
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.

2 participants