Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is import func, struct, class, and @_exported in Swift?

In Apple's github for the Swift Package manager they use

import func POSIX.isatty
import func libc.strerror_r
import var libc.EINVAL 
import var libc.ERANGE
import struct PackageModel.Manifest

source

There is also a file where the only code in it is @_exported source

@_exported import func libc.fileno

Is this a Swift 3 feature? I can not find anywhere that you can import a type in the Swift documentation and nothing on @_exported.

like image 765
lostAtSeaJoshua Avatar asked Dec 06 '16 16:12

lostAtSeaJoshua


1 Answers

You can import only a specific part of a module, not a whole module:

Providing more detail limits which symbols are imported—you can specify a specific submodule or a specific declaration within a module or submodule. When this detailed form is used, only the imported symbol (and not the module that declares it) is made available in the current scope.

From Import Declaration

For example import func POSIX.isatty will import function isatty from module POSIX instead of importing the whole module POSIX (which is BIG).

The @_exported attribute starts with an underscore. That means it's a private Swift attribute. Not a feature, an implementation detail. In short, this attribute lets you export a symbol from another module as if it were from your module.

like image 59
Sulthan Avatar answered Dec 19 '22 04:12

Sulthan