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

非 Y 不嫁゛ 提交于 2019-12-12 07:34:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41000256/what-is-import-func-struct-class-and-exported-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!