How do i access a same-named item in a surrounding scope?

/// The maximum number of elements per segment.
let maxElementsPerSegment = 128

/// An array that should have its elements in segments, to gain the memory advantages of linked lists.
struct SegmentedArray<T> {

    /// The count of elements typically in a segment.
    static var maxElementsPerSegment: Int { return ???.maxElementsPerSegment }


How can I use the global maxElementsPerSegment as part of the definition of the type’s version? Or is there no way to undo hiding? (I just renamed the global one for my workaround.)

I guess we need something like C++'s “::” prefix operator.

If it is a top-level declaration, then you can write the module name, such as Swift.min.

However, if it is something like the following, then I believe you are out of luck:

func foo() {
  var i = 0        // declaration 1
  func bar() {
    var i = 2
    // cannot access declaration 1 here

Now I have to figure out the name of the module from within the module. You have to look what’s set up in the Xcode preferences and copy the string, right?

I think we need to allow “Self” (or maybe a new semi-reserved identifier) within a type to refer to itself, so we don’t have to do wide-range find & replace whenever we rename the type. And a similar identifier for the current module. And finally: identifiers have to be unique within scopes within any function that isn’t nested in a code block. (So your “bar.i” would be banned.)

I do find it a bit sad that overload resolution never looks into the enclosing scope in these cases.

How would you disambiguate between referring to something from two different levels of enclosing scopes like in Nevin’s example?

If maxElementsPerSegment is a global constant or simply at file level you can use the module name to reference it when you need a way to disambiguate:

// at file level
let something = ...
struct MyType {
   var something: SameType { return MODULE_NAME.something }

In Xcode the module is by default called the same as the target you’re working on. I usually rename the app module to App which makes it really when you read App.constant.