Skip to content

Commit b34b99e

Browse files
committed
Fix method overloading ambiguity in Result class
## Issue Crystal does not support method overloading based solely on whether a block is passed. We had duplicate method definitions: ```crystal def track(node : Node(T)) # Without block def track(node : Node(T)) # With yield - AMBIGUOUS! ``` This caused compilation errors: - "method 'track' already defined" - Ambiguous method resolution ## Fix Use explicit `&block` parameter to differentiate methods: ```crystal def track(node : Node(T), &block) # Explicitly accepts block def track(node : Node(T)) # No block ``` ## Changes **src/oak/result.cr**: - `track(node, &block)` - For callers passing blocks - `track(node)` - For direct calls without blocks - `use(node, &block)` - For callers passing blocks - `use(node)` - For direct calls without blocks ## Method Ordering Placed block-accepting versions FIRST because: 1. Crystal resolves methods in definition order 2. Block version is more specific (requires &block) 3. Non-block version is the fallback ## Backward Compatibility ✅ All existing calls remain valid: - `result.track(node)` - calls non-block version - `result.track(node) { |r| ... }` - calls block version - `result.use(node)` - calls non-block version - `result.use(node) { |r| ... }` - calls block version ## Testing This fix resolves CI failures and allows: - ✅ Compilation to succeed - ✅ All specs to run - ✅ Type checking to pass The actual logic remains identical - only the method signatures changed to resolve Crystal's overload resolution requirements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8e0941b commit b34b99e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/oak/result.cr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,7 @@ struct Oak::Result(T)
128128
end
129129

130130
# :nodoc:
131-
def track(node : Node(T))
132-
@nodes << node
133-
self
134-
end
135-
136-
# :nodoc:
137-
def track(node : Node(T))
131+
def track(node : Node(T), &block)
138132
if @find_first
139133
yield track(node)
140134
self
@@ -146,14 +140,13 @@ struct Oak::Result(T)
146140
end
147141

148142
# :nodoc:
149-
def use(node : Node(T))
150-
track node
151-
@payloads.replace node.payloads
143+
def track(node : Node(T))
144+
@nodes << node
152145
self
153146
end
154147

155148
# :nodoc:
156-
def use(node : Node(T))
149+
def use(node : Node(T), &block)
157150
if @find_first
158151
yield use(node)
159152
self
@@ -164,6 +157,13 @@ struct Oak::Result(T)
164157
end
165158
end
166159

160+
# :nodoc:
161+
def use(node : Node(T))
162+
track node
163+
@payloads.replace node.payloads
164+
self
165+
end
166+
167167
private def clone
168168
self.class.new(@nodes.dup, @params.dup, @find_first)
169169
end

0 commit comments

Comments
 (0)