@CTMacUser (Daryle Walker) posted a new topic, subject "How do i access a same-named item in a surrounding scope?". It read:

   [code]
        /// 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 }
        
        }
        
    [/code]
    
    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 "`8b7c:f320:99b9:690f:4595:cd17:293a:c069`" prefix operator.

To which @Nevin replied

    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:
    [code] 
        func foo() {
          var i = 0        // declaration 1
          func bar() {
            var i = 2
            // cannot access declaration 1 here
          }
        }
        
    [/code]

So @CTMacUser (Daryle Walker) replied

    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.)

To which @Nobody1707 (Nobody1707) replied

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

To which @anthonylatsis (Anthony Latsis) replied

    Nobody1707:
    
    > 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?

To which @DevAndArtist (Adrian Meister) replied

    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:
    [code] 
        // at file level
        let something = ...
        struct MyType {
           var something: SameType { return MODULE_NAME.something }
        }
        
    [/code]
    
    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`.


{'source': 'discourse', 'url': 'https://forums.swift.org/t/how-do-i-access-a-same-named-item-in-a-surrounding-scope/11915', 'title': 'How do i access a same-named item in a surrounding scope?', 'views': 442, 'has_accepted_answer': False, 'word_count': 385, 'like_count': 2, 'reply_count': 3, 'participant_count': 5, 'posts': 6, 'tags': '', 'author': '@CTMacUser (Daryle Walker)', 'participants': '@CTMacUser (Daryle Walker), @Nevin, @Nobody1707 (Nobody1707), @anthonylatsis (Anthony Latsis), @DevAndArtist (Adrian Meister)', 'image_url': [None], 'created_at': '2018-04-16T02:42:15.619Z', 'last_posted_at': '2018-04-16T15:34:17.474Z'}