Swift Namespaces and Modules

I experimented with Swift on the way home from WWDC. In my opinion, one of the most attractive new features of Swift was the namespace. I was not able to get it to work, as I expected this to happen. Please see the attached screenshot and let me know if you have an idea of ​​what I'm doing wrong.

Declaring module global functionAttempting to utilize module function EDIT: Of course, I tried to remove the import statement.

+6
source share
2 answers

It turns out this is a known bug: https://devforums.apple.com/message/976286#976286

+4
source

Sorry, if I search for “namespace” or “namespacing” in Swift-eBook, there are no results. Maybe you can give me a link to an additional resource?

I would solve your problem simply using the struct and static functions.

Classa

 struct ClassA { static func localize() { println("Localized String") } } 

Classb

You can remove the import and execute the ClassA function as follows:

 ClassA.localize() 


Second way

In your case, you can also just do a line extension like this:

 extension String { func localize() -> String { return self+"-localized" } } println("Test".localize()) // prints "Test-localized" 
0
source

Source: https://habr.com/ru/post/970528/


All Articles