Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

Import of packages

Version 1
Created 2011-10-18
StatusDraft
Last modified 2011-10-18
Author Martin Nowak

Abstract

Provide means to import a whole package.

Rationale

A way of importing a package is proposed. This provides finer grained control of organizing packages and submodules and simplifies their usage. It will further allow to decouple a package’s interface from it’s internal organization.

Description

An import that does not resolve to a D source file but to a directory that has a _.{d|di} file will be treated as a package import.

Given the following files.

root
— pkg
: — _.d
   module pkg._;
   
   public import pkg.a;
   public import pkg.b : bar;
   
   int b;
   
   static this()
   {
       b = 12;
   }
: — a.d
   module pkg.a;
   
   void foo() {}
: — b.d

   module pkg.b;        void bar() {}    void baz() {}

The following behaviors are proposed.

Package import.

   import pkg;

   auto a = b; // b is pkg._.b    foo();      // foo is pkg.a.foo    bar();      // bar is pkg.b.bar        baz();        // error undefined identifier baz    import pkg.b; // error pkg.b is int pkg._.b    import pkg.a; // ok will import root/pkg/a.d        // this is equivalent to    import pkg._;

Static package import.

   static import pkg;        auto a = pkg.b; // pkg.b is pkg._.b    pkg.foo();      // pkg.foo is pkg.a.foo    pkg.bar();      // pkg.bar is pkg.b.bar    pkg.baz();      // error undefined identifier pkg.baz    import pkg.b;   // error pkg.b is int pkg._.b    import pkg.a;   // ok will import root/pkg/a.d        // pkg is the package with merged in scope of pkg._    // this has no direct equivalence but is roughly    static import pkg = pkg._;

Aliasing the package.

   import util=pkg;        auto a = util.b; // util.b is pkg._.b    util.foo();      // util.foo is pkg.a.foo    util.bar();      // util.bar is pkg.b.bar    util.baz();      // error undefined identifier util.baz    import pkg.b;    // ok will import root/pkg/b.d    import util.a;   // ok will import root/pkg/a.d        // util is the package pkg with merged in scope of pkg._    // this has no direct equivalence but is roughly    import util = pkg._;

Qualified import of submodule.

   import pkg.b; // will import root/pkg/b.d        baz();        // baz is pkg.b.baz    auto a = b;   // error undefined identifier b

   // this is equivalent to    static import __hidden = pkg._;    import pkg.b;

Any of the these will execute the static initializer of pkg._.

Impact

The idiom of explicitly importing _.d is already used. This will not conflict with existing code as it is either an error now or doesn’t change the scope members.

This document has been placed in the Public Domain.