# Import declarations

**URL:** <https://forums.swift.org/t/import-declarations/39562>\
**Category:** Using Swift\
**Created:** [August 21, 2020, 12:09am UTC](https://forums.swift.org/t/import-declarations/39562 "2020-08-21T00:09:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [August 21, 2020, 12:09am UTC](https://forums.swift.org/t/import-declarations/39562/1 "2020-08-21T00:09:50Z")

</div>

It is possible to have import declarations of class members? Or it just works for global symbols?

Example:  
UILabel has a member `shadowOffset: CGSize` and we have an extension on UIView that declares the extension on an external module. So I thought it would be possible to disambiguate that using a declaration import e.g.

```swift
import var UIKit.UILabel.shadowOffset 

```

But seems like it's not possible... not sure if thats something wrong on the syntax because its  
import `kind` `module`.`symbol name`

Its that possible to do?

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [August 21, 2020, 5:52pm UTC](https://forums.swift.org/t/import-declarations/39562/2 "2020-08-21T17:52:22Z")

</div>

I suspect that's not an intended use case. One of the things that the `import import-kind` declaration does is disambiguation; in case there are two identically named declarations coming from different imported modules, the one explicitly imported using `import import-kind` will be preferred.

However, with a non-top-level declaration like `UIKit.UILabel.shadowOffset`, such an ambiguity is unlikely to arise in practice.

You could solve the same issue with `let shadowOffset = UIKit.UILabel.shadowOffset` though.

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [August 21, 2020, 6:09pm UTC](https://forums.swift.org/t/import-declarations/39562/3 "2020-08-21T18:09:27Z")

</div>

Hey @Varun\_Gandh, thanks for the answer :))

> [@typesanitizer](#):
>
> However, with a non-top-level declaration like `UIKit.UILabel.shadowOffset` , such an ambiguity is unlikely to arise in practice.

Yes, that exactly our case, we have an open-source library that declares a public extension on `UIView` named `shadowOffset: CGSize` so users in installing our module get an ambiguous when using it in `UILabel`, as you mentioned when explicit importing both `UIKit` and `SwifterSwift` ... so my idea was to use declaration imports to disambiguate and choose one of them. Here is our issue with more details [shadowOffset for UILabel is conflict with UILabel's property. · Issue #622 · SwifterSwift/SwifterSwift · GitHub](https://github.com/SwifterSwift/SwifterSwift/issues/622) :)

> [@typesanitizer](#):
>
> You could solve the same issue with `let shadowOffset = UIKit.UILabel.shadowOffset` though.

This seems to give an error where this member is only allowed to be used on an instance of UILabel  
`Instance member 'shadowOffset' cannot be used on type 'UILabel'; did you mean to use a value of this type instead?`

I'm using Swift 5.3 Xcode 12 Beta 1

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [August 21, 2020, 6:32pm UTC](https://forums.swift.org/t/import-declarations/39562/4 "2020-08-21T18:32:53Z")

</div>

Sorry, I mistakenly thought it was a static property, but it is an instance property. Hmm, yeah this is kind of unfortunate.

IMO adding a new property with a non-colliding name would be the best way because that also makes it easier for a reader to understand whether the one on UILabel is being used or the one on UIView is being used.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 21, 2020, 6:35pm UTC](https://forums.swift.org/t/import-declarations/39562/5 "2020-08-21T18:35:11Z")

</div>

This is a known issue with name lookup in Swift. I believe the most recent discussion about a fully-qualified name syntax was [here](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482), which would allow you to write something like `myLabel.SwiftierSwift::shadowOffset` or `myLabel.UIKit::shadowOffset` to pick out the desired property unambiguously (the precise syntax, as you can read in that thread, is highly debated 🙂).

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [August 21, 2020, 6:41pm UTC](https://forums.swift.org/t/import-declarations/39562/6 "2020-08-21T18:41:44Z")

</div>

Scoped imports (imports targeting a particular declaration) are widely misunderstood. Two things you should know about them:

1. Their primary, intended use is _not_ to import one declaration from the module and leave the others un-imported; it's to _resolve an ambiguity_ between two modules which declare things with identical names. For instance, suppose `DatabaseKit` and `MusicKit` both define a type called `Record`. `import struct DatabaseKit.Record` ensures that uses of `Record` in that file will get the type from `DatabaseKit`, not the one from `MusicKit`.

2. This disambiguation only works for top-level declarations—nested declarations are looked up differently and scoped imports couldn't adjust which one you saw if there was a conflict—so that's the only thing scoped imports support. In other words, `import class UIKit.UILabel` is valid, but `import var UIKit.UILabel.shadowOffset` is not.

---

<div class="post-metadata">

**Author:** ![LucianoPAlmeida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucianopalmeida/32/2134_2.png) [@LucianoPAlmeida](https://forums.swift.org/u/LucianoPAlmeida)\
**Post date:** [August 21, 2020, 11:37pm UTC](https://forums.swift.org/t/import-declarations/39562/7 "2020-08-21T23:37:27Z")

</div>

Thank you for the answers @typesanitizer @Jumhyn @beccadax!
