I know that Swift doesn't use namespaces, but that names are defined within each module. First of all, I don't understand very well how this avoids name collisions -feel free to elaborate.
Nevertheless, my main question is: Let's say I want a tree structure without using NSTreeNode, so I make my own class named "TreeNode". Now let's say that Apple decides to include a class to build trees in Swift's standard library, and, as expected, they name it "TreeNode". What happens then? My custom TreeNode will collide with Standard Library's TreeNode... Will I have to change all my code in cases like that? Or maybe Apple will promise that Swift's Standard Library will not change in the future?
EDIT: Question answered here (thanks @Martin R for your comment)
Namespacing in Swift is implicit. All classes and other symbols belong to the target (module) they are defined in. So if you define a class String
the fully qualified name would be MyTarget.String
. When there is a name collision, you have to prefix the class name with the module (framework) it is defined in, except when there is a class with that name defined in the current module - this class takes precedence and does not need to be prefixed.
struct String {
var swiftString = ""
}
var a = String()
var b = Swift.String()
So if you create your class TreeNode
and Apple later adds a TreeNode
as well, your name would take precedence if you are using only one module and you wouldn't need to change anything. If you would want to use Swift's TreeNode
, you would need to refer to it as Swift.TreeNode
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With