Context-Aware Scoped Extensions

Proposal Idea: Context-Aware Scoped Extensions in Swift for Flexible Contextual Logic

Hello everyone,

I have a preliminary idea for enhancing Swift’s syntax and would love to share it with the community to discuss its feasibility. I’m not a compiler expert, so I’m eager to hear feedback, especially from those with expertise in compiler implementation!

Background and Motivation

In my development work, I often encounter situations where an object needs to access properties or logic specific to its containing context. For example, I have a Path class with an array of Anchor objects, and I want to check if certain Anchor instances are at the start or end of the Path.anchors array. The current approach looks like this:

class Anchor: Identifiable {
    var id = UUID()
    var center: CGPoint
    var isActive: Bool
    init(center: CGPoint, isActive: Bool) {
        self.center = center
        self.isActive = isActive
    }
}

class Path {
    var anchors: [Anchor]
    
    init(anchors: [Anchor]) {
        self.anchors = anchors
    }
    
    func test() {
        let activeAnchors = anchors.filter { $0.isActive }
        if activeAnchors.count == 2,
           activeAnchors[0] == anchors.first,
           activeAnchors[1] == anchors.last {
            print("You selected in the correct order")
        }
    }
}

The comparisons == anchors.first and == anchors.last are verbose and rely on reference equality, which can be error-prone if the collection changes. This is just one example of a broader problem: objects often need to query or interact with their container’s state in a repetitive or boilerplate-heavy way. I’m wondering if there’s a cleaner way to let objects like Anchor access their container’s context directly.

My Idea

I’m imagining a new Swift extension syntax, tentatively called extension where Scope == Type, which would allow a type to define properties or methods that are only available in the context of a specific container type. For example:

extension Anchor where Scope == Path {
    var isFirst: Bool {
        self == anchors.first
    }
    
    var isLast: Bool {
        self == anchors.last
    }
}

This would simplify the test() method to:

func test() {
    let activeAnchors = anchors.filter { $0.isActive }
    if activeAnchors.count == 2,
       activeAnchors[0].isFirst,
       activeAnchors[1].isLast {
        print("You selected in the correct order")
    }
}

The Scope == Path constraint would tell the compiler that Anchor is operating in the context of Path.anchors, allowing direct access to the anchors collection to check if self is the first or last element. This syntax could apply to many scenarios where an object needs to interact with its container’s state, reducing repetitive code and improving readability.

Why This Matters

  • Conciseness: Eliminates boilerplate like manual comparisons, making code more intuitive.
  • Generality: This pattern appears in many domains (e.g., graphics, data structures, UI components) and could benefit a wide range of use cases.
  • Type Safety: The compiler could ensure contextual properties or methods are only used in the appropriate container context.

What I’m Unsure About

As someone not well-versed in compilers, I’m uncertain about the implementation details:

  • Is direct access to anchors feasible? What special handling would the compiler need?
  • Would this syntax introduce performance overhead or complexity?
  • Are there existing Swift features (like protocols, generics, or associated objects) that could achieve this more easily?

Discussion Questions

  1. Is this Scope syntax feasible in Swift’s type system? What are the technical challenges?
  2. Are there existing Swift features that could elegantly solve this general problem of context-aware logic?

Thanks for your feedback and suggestions!

1 Like