How does D’s ‘import’ work?

H. S. Teoh hsteoh at qfbox.info
Wed May 31 18:56:02 UTC 2023


On Wed, May 31, 2023 at 06:43:52PM +0000, Cecil Ward via Digitalmars-d-learn wrote:
> Is there an explanation of how D’s ‘import’ works somewhere? I’m
> trying to understand the comparison with the inclusion of .h files,
> similarities if any and differences with the process.

Unlike C's #include, `import` does NOT paste the contents of the
imported file into the context of `import`, like #include would do.
Instead, it causes the compiler to load and parse the imported file,
placing the parsed symbols into a separate symbol table dedicated for
that module (in D, a file == a module). These symbols are then pulled
into the local symbol table so that they become available to code
containing the import declaration.

(There's a variation, `static import`, that does the same thing except
the last step of pulling symbols into the local symbol table. So the
symbols will not "pollute" the current namespace, but are still
accessible via their fully-qualified name (FQN), i.e., by the form
`pkg.mod.mysymbol`, for a symbol `mysymbol` defined in the module
`pkg.mod`, which in turn is a module under the package `pkg`.)

For more information:

	https://tour.dlang.org/tour/en/basics/imports-and-modules
	https://dlang.org/spec/module.html


T

-- 
People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. -- D. Knuth


More information about the Digitalmars-d-learn mailing list