Skip to content

Enable nested control flow blocks#12

Merged
fengb3 merged 1 commit into
mainfrom
fix/issue-11-nested-control-flow
Jun 9, 2026
Merged

Enable nested control flow blocks#12
fengb3 merged 1 commit into
mainfrom
fix/issue-11-nested-control-flow

Conversation

@fengb3

@fengb3 fengb3 commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #11

Adds If, Else, ElseIf, For, While, DoWhile, Foreach, Switch, and Try extension methods on CodeOption (the base class) so control flow constructs can be nested inside any block.

Before (broken)

m.For(f =>
{
    f.Switch(s => { ... }); // ❌ Compile error: ForOption has no Switch method
});

After (works)

m.For(f =>
{
    f.Switch(s =>           // ✅ Now works via CodeOption extension
    {
        s.Case(c =>
        {
            c.If(i => { ... });  // ✅ Deep nesting works too
        });
    });
});

Changes

  • Code.cs: Added 9 control flow extension methods on CodeOption following the same AddChild pattern as existing methods
  • ControlFlowTests.cs: Added 5 new tests covering nesting scenarios:
    • ForContainingSwitch_Issue11 — the exact example from the issue
    • DeepNesting_ForSwitchCaseIf — 3 levels deep
    • WhileContainingFor — loop nesting
    • TryInsideFor — exception handling inside a loop
    • IfElseIfElseInsideFor — conditional branching inside a loop

Backward Compatibility

The existing MethodOptionExtensions methods are not modified. C# overload resolution prefers the more-specific MethodOption overload when called on a MethodOption, so all existing code continues to work unchanged.

Test Results

All 81 tests pass (76 existing + 5 new).

🤞 Generated with Claude Code

Add If, Else, ElseIf, For, While, DoWhile, Foreach, Switch, and Try
extension methods on CodeOption (base class) so control flow constructs
can be nested inside any block (e.g. Switch inside For, If inside Case).

The existing MethodOption-specific extensions are preserved for backward
compatibility. Includes tests for nesting patterns.

Fixes #11

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@fengb3 fengb3 force-pushed the fix/issue-11-nested-control-flow branch from 7a26551 to 2bf13f6 Compare June 9, 2026 08:21
@fengb3 fengb3 merged commit 7a80596 into main Jun 9, 2026
4 checks passed
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.

控制流 block API 只能在 MethodOption 上使用,无法在 For/Switch/Case 等 block 内部嵌套

1 participant