Last update October 18, 2011

Language Devel / DIPs /
DIP15



Difference (last change) (Author, normal page display)

Added: 73a74,77
import pkg.a; // ok will import root/pkg/a.d

// this is equivalent to
import pkg._;

Added: 85a90,94
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._;

Added: 97a107,111
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._;

Added: 106a121,124

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

DIP15: Import of packages

DIP:15
Title:Import of packages
Version:1
Status:Draft
Created:2011-10-18
Last Modified:2011-10-18
Author:Martin Nowak
Links: 

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.

Copyright

This document has been placed in the Public Domain.


FrontPage | News | TestPage | MessageBoard | Search | Contributors | Folders | Index | Help | Preferences | Edit

Edit text of this page (date of last change: October 18, 2011 11:02 (diff))