Swift - import all of framework VS part of it

Gui Reis

My background is from C and Python and when I import in these languages it is always a good practice to only import what will be used, mainly in Python. In Swift I know there is a way to do this, but does it really make any difference?

When I'm going to do some extension, some model or enum I only care what will be needed.. But does it really matter? It makes a difference?

Example of my code:

/* Gui Reis    -    [email protected] */

/* Libraries */
// import CoreData
import struct CoreData.UUID


struct Garden {
    let id: UUID
    let title: String
    let bio: String
}

My question is with a focus on performance mainly: does it make a difference to the compiler, since I'm specifying exactly what I want to import and I'm not importing anything else.

I believe the compiler should already do this filter, but me doing it, does it make a significant difference? Is it a good practice in Swift?

Sweeper

The feature of importing just a single symbol from a module is mainly for avoiding name conflicts.

For (a very contrived) example, let's say there is a module A which declares the classes Foo, Bar, and a module B which declares the classes Bar, Baz.

In your file, you import A and use Foo and Bar many times. Later, there is a change that requires you to use B.Baz. At this point, importing the entire B would make Bar ambiguous between A.Bar and B.Bar, and you would have to change all existing occurrences to A.Bar, when all you wanted to use was B.Baz. To solve this problem, you could say import class B.Baz instead.

As for performance, while importing just a single symbol does potentially make the job for the compiler easier, as it does not need to search as much to resolve a name that you have written. However, I highly doubt that this is a significant difference compared to all the other more complicated things that the compiler does, such as overload resolution and type inference.

This also makes no difference at all at runtime, because import declarations does not determine what frameworks are linked in the compiled binary as you seem to be implying. import declarations are simply a compile time thing that determine what names are available in the file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript import/as vs import/require?

`from ... import` vs `import .`

file viewController.swift is part of module "coreData" ignoring import

How to import and use Swift Pod Framework in Objective-C Project

Import Framework in Swift Project, Xcode

@import vs #import - iOS 7

import Swift vs import Foundation

How to import private framework headers in a Swift framework?

Import Swift framework in Swift: "Use of undeclared type 'MyCustomView'"

How to import opencv2 framework in iOS Swift

iOS import swift class from swift using framework

Create and import swift framework

Import Objective-c framework into Swift framework (Google Analytics + Cocoapod)

Import Objective-c framework into Swift framework project

Import my custom module/framework Xcode Swift

Import Objective-C Framework (CocoaPod) into Swift?

How do you selectively import a framework in Swift?

iOS Swift framework: How to import Objective C code into swift framework properly?

Xcode: File 'GameViewController.swift' is part of module 'SceneKit'; ignoring import

Xcode Swift Import Code Within "an App Target" vs. "a Framework Target" -- What is difference?

Import part of a repository as lib

Import Swift framework to Obj-C

import .framework inside SPM (Swift Package Manager)

Import Facebook SDK inside Framework in Swift

Swift import CoreGraphics.CGBase VS. import CoreGraphics causes LLDB malfunctioning

Is C a part of the .NET framework?

Import External part of html

Conditionally import a framework (such as Speech) based on iOS Version in Swift?

How to get the information about all groupChats in which my bot is part of. MsBot framework for Teams (nodejs)