RFC: Polymorphic Set Operations (intersect, difference, union) #97
Replies: 3 comments 1 reply
-
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the decisions! Here's my analysis of edge cases we should consider: Edge Cases for Deep Object Comparison1. Type Mismatches at Same KeyRecommendation: Empty result 2. Null vs Missing KeysRecommendation: 3. Empty Arrays/ObjectsRecommendation: Yes, include 4. Array Order SensitivityRecommendation: 5. Circular ReferencesRecommendation: Detect cycles and throw error. Deep recursion with cycles = infinite loop. 6. Very Deep NestingRecommendation: No arbitrary depth limit. Trust the runtime. If stack overflows, it's a user problem. Edge Cases for Strings7. Unicode and Grapheme ClustersRecommendation: Use Swift's native 8. Case SensitivityRecommendation: Case-sensitive by default. 9. Empty StringRecommendation: Result is Edge Cases for Lists10. Duplicate ElementsRecommendation: Alternative: This is the most important edge case to decide. 11. Mixed Types in ListsRecommendation: 12. Nested Objects in ListsRecommendation: Summary of Recommendations
Open Question: DuplicatesThe duplicate handling (edge case #10) is the most significant design decision remaining: Option A: Set semantics (deduplicate)
Option B: Multiset semantics (preserve duplicates)
My recommendation: Option A (set semantics) for consistency with the decision that What do you think? |
Beta Was this translation helpful? Give feedback.
-
Proposal & Issue CreatedBased on our discussion, I've created: ProposalARO-0042: Polymorphic Set Operations The proposal incorporates all decisions from this discussion:
Implementation TrackingIssue: #98 Feature BranchBranch: Ready for implementation when approved. No PR created yet per request. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Statement
Given two lists:
[2, 3, 5][1, 2, 3, 4]We want to compute:
[2, 3](elements in both)[5](elements in A but not B)[1, 4](elements in B but not A)Currently, ARO has no built-in set operations. This RFC proposes adding them as polymorphic Compute operations that work across Lists, Strings, and nested Objects.
Design Goals
ComputationServiceProposed Syntax
Type Behavior Matrix
Implementation Approaches Considered
We evaluated three approaches for adding set operations to ARO. Below is a detailed comparison with pros, cons, and our recommendation.
Approach 1: Compute Operations ✅ RECOMMENDED
Add
intersect,difference,unionas operations within the existingComputeAction, following the polymorphic pattern already established for operations likelength,uppercase, andhash.Syntax:
Pros
lengthreturns count for strings, arrays, and dictsComputeis already familiar<result: operation>) already establishedComputationServiceprotocol - no ActionProtocol neededas?) handles different types elegantlyCons
from...withsyntax already supported; just need implementationApproach 2: New Actions (IntersectAction, DifferenceAction) ❌ NOT RECOMMENDED
Create dedicated ActionImplementation structs for each set operation.
Syntax:
Pros
<Intersect>is immediately clear what it doesCons
length,uppercase) are Compute ops, not separate actionsActionImplementationprotocol instead of simpleComputationServiceWhy Not Chosen
This approach treats set operations as verbs (things you do) rather than computations (transformations). In ARO's design philosophy:
Set operations are clearly transformations, not world-interactions. Using Compute aligns with the language's conceptual model.
Approach 3: Filter with⚠️ PARTIAL SOLUTION
in/not inOperatorsEnhance the existing Filter action to support array membership testing.
Current limitation:
Proposed enhancement:
Pros
where <field> in <list>reads naturallynot inis usefulCons
[2, 3, 5]directly[{v: 2}, {v: 3}]instead of[2, 3]"hello"and"bello"Verdict
Useful enhancement, but NOT a complete solution. We should add
not inand array support to Filter regardless, but this doesn't replace the need for proper set operations in Compute.Comparison Summary
Final Decision: Compute Operations + Filter Enhancement
We recommend:
intersect,difference,unionas Compute operationsnot inoperator and array support to FilterThis provides:
Complete Example
Plugin Extensibility
The existing
ComputationServiceprotocol allows plugins to add custom set operations:No architecture changes needed - the design is already extensible.
Files to Modify
Sources/ARORuntime/Actions/BuiltIn/ComputeAction.swiftSources/ARORuntime/Actions/BuiltIn/QueryActions.swiftnot inoperator, makeinaccept arraysFiles to Create
Examples/SetOperations/main.aroQuestions for Discussion
uniondeduplicate by default, or preserve duplicates?symmetric-differencebuilt-in, or leave it to plugins?Feedback welcome!
Beta Was this translation helpful? Give feedback.
All reactions