Last update January 23, 2007

Doc Comments /
Module



Modules

More complex examples

Collect multiple modules under a single name:
import all = std.string;
import all = std.stdio; 

int main()
{
  int i,j;
  i = all.atoi("45");
  j = atoi("32"); //Compiler Error: Undefined Identifier
  
  all.writefln("The int is %d",i);
  writefln("Global writefln"); //Compiler Error: Undefined Identifier
  return 0;
}
You can add specific identifiers to the global namespace...
import all = std.string;
import all = std.stdio : writefln; 

int main()
{
  int i,j;
  i = all.atoi("45");
  //j = atoi("32"); //Compiler Error: Undefined Identifier
  
  all.writefln("The int is %d",i);
  writefln("Global writefln"); 
  return 0;
}

More Information

Using Filenames that Aren't Valid Module Identifiers

If you use a source file with a filename that isn't a valid identifier in D (such as "foo-bar.d"), you need to add a module statement with a valid identifier to get it to compile:

 module foobar;
from NG:digitalmars.D/29764

Invalid Module Names

D reserves the module name of "object" (e.g. "object.d") for the built-in object base class which is automatically imported. ( NG:digitalmars.D.announce/4313), but there might be a work-around if it has to be called "object" ( NG:digitalmars.D.announce/4318).

Keywords can't be part of a module name at all since module and package names are identifiers. For example, given "import std.stdio;", the "std" and "stdio" are both identifiers.

Unittest keyword

It seems like this page would be a good place to explain the use of the unittest keyword (since unit tests can exist in modules as well as classes). -- JustinCalvarese

Examples

Message

Put your comments about the official/non-official page here.

Identifer Recognition

This looks like a typing error to me:
 import foo;
  ...
  q = y;		// sets q to foo.y
Shouldn't y be foo.y? -- PeterEriksen?

If only one "y" identifier has been declared (or imported), the compiler shouldn't have a problem finding it. When only foo is imported, D figures out that the desired "y" is from the "foo" module. Looks fine to me. -- JustinCalvarese

Links


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

Edit text of this page (date of last change: January 23, 2007 23:10 (diff))