Scoped import of modules in Swift

Originator:kare
Number:rdar://18035792 Date Originated:15.08.2014
Status:Open Resolved:No
Product:Swift Product Version:Beta 5
Classification:Feature request Reproducible:Yes
 
Summary:
Swift only allows importing modules in the global scope (see https://devforums.apple.com/thread/240355?tstart=0 ).

I love how customisable Swift is and all the interesting stuff people have been doing with it. But I would be hesitant to import a module which does extensive overloading of standard functions and operators, because it can become difficult to see which code is actually being called at any one time. The risks for vague and hard to debug side-effects are often too high.
 
Especially when it comes to implementing Domain Specific Languages (DSLs) in Swift it would be nice to be able to really go at it and make significant additions/alterations. It's risky to do that now because importing such a module could mess up the entire project.
 
And independent modules can define the same new operators, overload the same functions, add extensions with the same names or in other ways interfere with each other.
 
 
I think a simple solution to all this is to not import modules to the global scope only. Say if you wanted to use a cool HTML parsing module:
if let html = download(URL) {
     import FreakyHTMLParser
     
     // do some cool parsing

} else {
     // something else
}

Here the FreakyHTMLParser module would only have an effect from the import statement to the end of the scope (basically to the next "}" ). This way the user of a module has more control over his own code, and the writer of a module does not have to worry so much about side effects. It is a safe and contained way of importing modules.

Steps to Reproduce:
if let html = download(URL) {
     import FreakyHTMLParser
     
     // do some cool parsing

} else {
     // something else
}


Expected Results:
It should compile, and the FreakyHTMLParser module is only available from the import statement until the next "}".

Actual Results:
Compile error saying importing is only allowed in the global scope.

Version:
Xcode beta 5

Notes:


Configuration:


Attachments:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!