# Explicity refer to global variable?

**URL:** <https://forums.swift.org/t/explicity-refer-to-global-variable/35325>\
**Category:** Using Swift\
**Created:** [April 13, 2020, 12:02am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325 "2020-04-13T00:02:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [April 13, 2020, 12:02am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/1 "2020-04-13T00:02:13Z")

</div>

Is there a way to refer to a global/module level variable from a scope where it is shadowed? I've seen things like `Swift.foo` before to refer to `foo` in the standard library. But in this example below, the code is inside a Swift package. Neither `Swift` nor my package name works in place of the `???` below.

```
private var logDebug: Bool = false

public class Foo {
    public static var logDebug: Bool {
        get { ???.logDebug } // trying to refer to the top level `logDebug` above
        set { ???.logDebug = newValue }
    }
}

```

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [April 13, 2020, 1:52am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/2 "2020-04-13T01:52:45Z")

</div>

Use the module name in place of `???`. If you are using SwiftPM, the module name is the same as the target name\*.

\*unless you named the target something that would be invalid as a module name, in which case SwiftPM will have sanitized it for you. e.g. `My Module` → `My_Module`

---

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [April 13, 2020, 2:02am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/3 "2020-04-13T02:02:16Z")

</div>

I tried that. The package and target both have the same name. I get the error: "Type 'MyModule' has no member 'logDebug'".

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 13, 2020, 2:28am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/4 "2020-04-13T02:28:05Z")

</div>

It may work if `logDebug` isn't `private`. I'd guess that, in marking it private, you've made it invisible to the module namespace.

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [April 13, 2020, 8:10am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/5 "2020-04-13T08:10:04Z")

</div>

I tested this two ways:

- I created a test framework in Xcode (using Xcode 11.4 btw), added a new file named `Foo.swift` with the code above, and then replaced `???` with the framework name. It built just fine.

- I create a new package in Xcode, replaced `MyLibrary.swift` with the code above, replaced `???` with `MyLibrary`, and it also built just fine.

I’m not sure what’s going on in your specific case, but I’m not seeing it. Can you run through the tests above to see if you can replicate my results?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [April 13, 2020, 9:51am UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/6 "2020-04-13T09:51:36Z")

</div>

> [@robnik](#):
>
> "Type 'MyModule' has no member 'logDebug'

If you read the error carefully you see you're actually trying to refer to a _type_ not module named `MyModule`. This happens because types currently take precedence in name lookup over modules, and you apparently reuse the same name for both in this case.

It's a [known shortcoming](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482) in Swift today. AFAICT your only options are to rename the type or module (a common pattern for the latter being `MyModuleKit`), or to rename the variable so that it needs no qualification.

---

<div class="post-metadata">

**Author:** ![robnik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@robnik](https://forums.swift.org/u/robnik)\
**Post date:** [April 13, 2020, 2:22pm UTC](https://forums.swift.org/t/explicity-refer-to-global-variable/35325/7 "2020-04-13T14:22:09Z")

</div>

I tried the simple tests as you suggested and it led me to the problem. 🤦‍♂️ Sorry to waste people's time. When you create a Swift Package in Xcode it creates a struct with the same name as your module. I had never deleted that file, and didn't notice it in the sea of almost 100 other files. I deleted that struct and everything works as expected.
