Is there any merit for using `import class/struct/enum/func...`?


Recently I found that using import class Foo.Bar I can import only certain class Bar in Foo.
I'm curious about whether using import class Foo.Bar has any merits than using import Foo, in terms of runtime performance, build performance, binary size, and so on. Can anyone tell me about this?

Thank you!

there should be no runtime difference between the two.

but a well-designed library should be namespaced enough that you should never have to import individual types to begin with. in fact these days i try to build libraries that you can import with an

@_exported import Foo

which makes all of its types visible in every file in your module and still not feel like your local namespace has been polluted.

that we have a feature like import class Foo.Bar is mostly a recognition that a lot of people (my past self included) have tended to write libraries that assume they are the center of the universe and vend types with common names like Identifier, Queue, Document, etc.

1 Like