Skip to content

Expandable switch - #49

Merged
ipapadop merged 3 commits into
mainfrom
expandable-switch
Aug 1, 2026
Merged

Expandable switch#49
ipapadop merged 3 commits into
mainfrom
expandable-switch

Conversation

@ipapadop

@ipapadop ipapadop commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Closes #23

@ipapadop ipapadop self-assigned this Aug 1, 2026
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Expandable deferred switch expressions with append() and unified result mapping

✨ Enhancement 🧪 Tests 📝 Documentation 🕐 20-40 Minutes

Grey Divider

AI Description

• Add switch_expression::append() to extend existing deferred switches with new cases.
• Replace conditional/switch-specific result mappers with a shared detail::map_result helper.
• Add unit/integration coverage for append semantics, variants, and move-only case bodies.
Diagram

graph TD
  U["User code"] --> S["switch_expression"] --> C["choose_case()"] --> M["detail::map_result"] --> R["result_type"]
  S --> A["append()"] --> S2["expanded switch"]
  T["Tests"] --> A
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Provide `extend(other_switch)` / switch composition
  • ➕ Allows combining two pre-built switches without unpacking cases
  • ➕ Potentially clearer for builder-style construction
  • ➖ More API surface area (composition rules, default precedence, type merging)
  • ➖ Harder to keep simple value-category (lvalue/rvalue) semantics correct
2. Implement append via `std::tuple_cat` + constructor forwarding
  • ➕ Can be more direct than std::apply for tuple manipulation
  • ➕ Might reduce lambda complexity
  • ➖ Still needs careful forwarding/copy semantics for owned vs referenced expressions
  • ➖ May increase template instantiation noise without clear readability gains

Recommendation: The chosen append() design (two overloads for const& and &&) is a good fit: it preserves reference semantics when copying and enables moving owned expressions when the source switch is an rvalue. Consider adding switch-to-switch composition only if users commonly need to merge pre-assembled switches; otherwise append() keeps the API minimal.

Files changed (8) +146 / -73

Enhancement (2) +92 / -33
map_result.hppAdd shared 'detail::map_result' utility +40/-0

Add shared 'detail::map_result' utility

• Introduces a reusable result-mapping helper that normalizes void/non-void evaluated values into the target result type (including 'std::monostate' for void branches).

include/deferred/detail/map_result.hpp

switch.hppAdd 'switch_expression::append()' and unify result mapping +52/-33

Add 'switch_expression::append()' and unify result mapping

• Adds 'append()' overloads for 'const&' and '&&' to create a new switch expression with additional cases, preserving evaluation order (existing cases first). Replaces the switch-specific result mapper with 'detail::map_result' and performs minor template cleanup ('tuple_size_v').

include/deferred/switch.hpp

Refactor (2) +6 / -40
conditional.hppRefactor conditional result mapping to shared helper +3/-28

Refactor conditional result mapping to shared helper

• Removes the conditional-specific 'map_conditional_result' and switches conditional evaluation to use 'detail::map_result' from a shared header.

include/deferred/conditional.hpp

while.hppSimplify while_expression call operators +3/-12

Simplify while_expression call operators

• Consolidates the call operator overload set by replacing the previous 'const&'/'&'/'&&' trio with a simpler 'const' and non-const overload pair while keeping behavior consistent.

include/deferred/while.hpp

Tests (2) +46 / -0
switch.cppAdd integration test for append() behavior +22/-0

Add integration test for append() behavior

• Adds an integration test validating that appended cases are considered after existing cases, and that the original switch remains unchanged when appending from a const reference.

test/integration/switch.cpp

switch.cppAdd unit tests for append() with variants and move-only bodies +24/-0

Add unit tests for append() with variants and move-only bodies

• Adds unit tests verifying that append supports heterogeneous result types (variant expansion) and that moving a switch into append works with move-only case bodies.

test/unit/switch.cpp

Documentation (2) +2 / -0
AGENTS.mdDocument expandable switch expressions +1/-0

Document expandable switch expressions

• Updates the project overview to explicitly mention that switch expressions can be expanded via 'append()'.

AGENTS.md

README.mdAdvertise expandable deferred switch expressions +1/-0

Advertise expandable deferred switch expressions

• Adds a README bullet highlighting expandable deferred switch expressions as a supported feature.

README.md

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d73ca9f3fa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread include/deferred/detail/map_result.hpp Outdated
@qodo-code-review

qodo-code-review Bot commented Aug 1, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. map_result.hpp guard mismatch ✓ Resolved 📘 Rule violation ⚙ Maintainability
Description
The new header guard macro is DEFERRED_DETAIL_MAP_RESULT_HPP, which does not follow the required
#ifndef DEFERRED_FILENAME_HPP naming format for the header name. This can break the repository’s
documented consistency requirements for include guards.
Code

include/deferred/detail/map_result.hpp[R4-5]

+#ifndef DEFERRED_DETAIL_MAP_RESULT_HPP
+#define DEFERRED_DETAIL_MAP_RESULT_HPP
Evidence
PR Compliance ID 8 requires header guards to use the DEFERRED_FILENAME_HPP naming format. The
added file include/deferred/detail/map_result.hpp defines #ifndef DEFERRED_DETAIL_MAP_RESULT_HPP
/ #define DEFERRED_DETAIL_MAP_RESULT_HPP, which does not match that required pattern for a
map_result.hpp header.

AGENTS.md: Headers Must Use the Required Header Guard Naming Format
include/deferred/detail/map_result.hpp[4-6]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`include/deferred/detail/map_result.hpp` uses the header guard `DEFERRED_DETAIL_MAP_RESULT_HPP`, but the required format is `DEFERRED_FILENAME_HPP` (where `FILENAME` is the header’s filename uppercased).

## Issue Context
This repository enforces a specific header guard naming scheme for consistency.

## Fix Focus Areas
- include/deferred/detail/map_result.hpp[4-6]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

Comment thread include/deferred/detail/map_result.hpp Outdated
@ipapadop
ipapadop merged commit 605282c into main Aug 1, 2026
5 checks passed
@ipapadop
ipapadop deleted the expandable-switch branch August 1, 2026 02:25
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.

Allow expansion of switch-case expressions

1 participant