[swift-evolution] Beef up Imports

T.J. Usiyan griotspeak at gmail.com
Sun Dec 27 09:30:08 CST 2015


*import* Foo *using* (bar, Baz, qux, waldo *as* fred)  // a "whitelist
import"
*import* Foo *hiding* (bar, Baz, qux, waldo *as* fred) // a "blacklist
import"



Is nice but `hiding… (waldo as fred)` is confusing. Am I hiding waldo? It
is also strange to look in that list for things that I am hiding and things
that I am importing. Context switches after the item that I am importing
because `as` follows the item in question.


*import* Foo (waldo *as* fred) *using* (bar, Baz, qux)  // a "whitelist
import"
*import* Foo (waldo *as* fred) *hiding* (bar, Baz, qux) // a "blacklist
import"


`using` didn't actually need the syntax change but maintaining one
consistent form might be worth it. If not, `import Foo using (bar, Baz,
waldo as fred)` is fine.

On Sun, Dec 27, 2015 at 7:44 AM, Pyry Jahkola via swift-evolution <
swift-evolution at swift.org> wrote:

> On 27 Dec 2015, at 07:12, Developer via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Therefore, I propose the introduction of 3 agda-esque operations for
> imports to replace the usual `import
> {func|typealias|struct|class|enum|etc.}` syntax:
>
> • import Foo using (bar, Baz, qux, corge, …)
> • import Foo hiding (bar, baz, qux, corge, …)
> • import Foo renaming (grault to garply, waldo to fred, …)
>
>
> +1, but why not just…?
>
> *import* Foo *using* (bar, Baz, qux, waldo *as* fred)  // a "whitelist
> import"
>
> *import* Foo *hiding* (bar, Baz, qux, waldo *as* fred) // a "blacklist
> import"
>
>
> I've been able to work around identifier conflicts with the present
> *import* syntax but it gets clumsy with larger modules. I think both
> *import*-*using* and *import*-*hiding* would be very welcome in Swift,
> but the third notation made me double check its semantics from The Agda
> Wiki. Turns out
> <http://wiki.portal.chalmers.se/agda/pmwiki.php?n=ReferenceManual.Modules#mods>
>  the *renaming* (...) syntax is actually an extension to the other two
> and is often used as a part of the one or the other:
>
> *import* Foo *using* (bar, Baz, qux) *renaming* (waldo *to* fred)
> *import* Foo *hiding* (bar, Baz, qux) *renaming* (waldo *to* fred)
>
>
> Why not do without *renaming*? Just allow both *import*-*using* and
> *import*-*hiding* to optionally *rename* imported identifiers. Further,
> while these keywords can and should be made context-specific, I think it's
> simplest to reuse "*as*" as the associating keyword.
>
> That would lead us to the *proposed syntax*:
>
> *import* Foo *using* (bar, Baz, qux, waldo *as* fred)  // a "whitelist
> import"
>
> *import* Foo *hiding* (bar, Baz, qux, waldo *as* fred) // a "blacklist
> import"
>
>
> where the tuple-like parentheses enclose a potentially empty list of
> identifiers or "identifier *as* identifier" mappings. The examples below
> should clarify the meaning and intended use of these two statements.
>
> Again, it should be obvious by uniqueness of identifiers what each one is
> referencing, so qualification of each identifier is unnecessary.
>
>
> Agreed. In addition, extending the import statement to class/enum/struct
> members doesn't make sense to me. Those don't conflict with the global
> scope at all.
>
> — Pyry Jahkola
>
>
> P.S. If this turns into a formal proposal, should we also address how
> imported operators should be dealt with? Can operators be selectively
> imported, hidden, or renamed? If renaming is made possible, we should be
> explicit on how the renamed operator inherits its associativity and
> precedence from the original one.
>
>
> *Examples*
>
> Suppose the module Foo contains the following identifiers:
>
> // module Foo:
> // - bar, Baz, qux, waldo
>
>
> The "import-all" remains as it is in Swift 2:
>
> *import* Foo
>
> _ = (bar, Baz.self, qux, waldo) // All identifiers can be unqualified.
>
>
> The "import-all" is synonymous with the longer "hide-nothing":
>
> *import* Foo *hiding* ()
>
>
> To hide or rename an identifier, fill in the parentheses:
>
> *import* Foo *hiding* (Baz, waldo *as* fred)
>
> _ = (bar, qux, fred)            // These names can be unqualified.
> _ = (Foo.Baz.self, Foo.waldo)   // These names must be qualified.
> _ = Foo.fred                    // Compiler error!
>
>
> To import selectively, potentially renaming identifiers, use the *import*-
> *using* syntax:
>
> *import* Foo *using* (Baz *as* FooBaz, qux, waldo *as* fred)
>
> _ = (FooBaz.self, qux, fred)    // These names can be unqualified.
> _ = (Foo.Baz.self, Foo.bar)     // These names must be qualified.
>
>
> Finally, it's possible to require qualified use of the Foo module:
>
> *import* Foo *using* ()
>
> _ = (Foo.bar, Foo.Baz.self, Foo.qux, Foo.waldo) // OK.
> _ = (bar, Baz.self, qux, waldo) // Compiler error x 4!
>
>
> End.
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151227/9cdb18b6/attachment.html>


More information about the swift-evolution mailing list