object

Forms the symbols available to all D programs. Includes Object, which is the root of the class object hierarchy. This module is implicitly imported.

Types 36

aliassize_t = typeof(int.sizeof)
aliasptrdiff_t = typeof(cast(void *) 0 - cast(void *) 0)
aliassizediff_t = ptrdiff_t
aliasnoreturn = typeof(* null)

Bottom type. See spec/type.

aliashash_t = size_t
aliasequals_t = bool
aliasstring = immutable(char)[]
aliaswstring = immutable(wchar)[]
aliasdstring = immutable(dchar)[]
classObject

All D class objects inherit from Object.

Methods
string toString()Convert Object to a human readable string.
size_t toHash() @trusted nothrowCompute hash function for Object.
int opCmp(Object o)Compare with another Object obj. Returns: this < obj < 0 this == obj 0 this > obj > 0
bool opEquals(Object o)Test whether this is equal to o. The default implementation only compares by identity (using the is operator). Generally, overrides and overloads for opEquals should attempt to compare objects by t...
Object factory(string classname)Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor. Returns: null if failed Example: --- module foo.bar;
structInterface

Information about an interface. When an object is accessed via an interface, an Interface* appears as the first entry in its vtbl.

Fields
TypeInfo_Class classinfoClass info returned by `typeid` for this interface (not for containing class)
void *[] vtbl
size_t offset

Array of pairs giving the offset and type information for each member in an aggregate.

Fields
size_t offset

Runtime type information about a type. Can be retrieved for any type using a expression,TypeidExpression, TypeidExpression.

Methods
string toString() const @safe nothrow
size_t toHash() @trusted const nothrow
int opCmp(Object rhs)
bool opEquals(Object o)
bool opEquals(const TypeInfo ti) @safe nothrow const
size_t getHash(scope const void * p) @trusted nothrow constComputes a hash of the instance of a type. Params: p = pointer to start of instance of the type Returns: the hash Bugs: fix https://issues.dlang.org/show_bug.cgi?id=12516 e.g. by changing this to a...
bool equals(in void * p1, in void * p2) constCompares two instances for equality.
int compare(in void * p1, in void * p2) constCompares two instances for <, ==, or >.
size_t tsize() @property nothrow pure const @safe @nogcReturns size of the type.
void swap(void * p1, void * p2) constSwaps two instances of the type.
inout(TypeInfo) next() @property nothrow pure inout @nogcGet TypeInfo for 'next' type, as defined by what kind of type this is, null if none.
const(void)[] initializer() nothrow pure const @safe @nogcReturn default initializer. If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned. For static arrays, this returns the defau...
uint flags() @property nothrow pure const @safe @nogcGet flags for type: 1 means GC should scan for pointers, 2 means arg of this type is passed in SIMD register(s) if available
const(OffsetTypeInfo)[] offTi() constGet type information on the contents of the type; null if not available
void destroy(void * p) constRun the destructor on the object and all its sub-objects
void postblit(void * p) constRun the postblit on the object and all its sub-objects
size_t talign() @property nothrow pure const @safe @nogcReturn alignment of type
immutable(void) * rtInfo() @property nothrow pure const @trusted @nogcReturn info used by the garbage collector to do precise collection.
Fields
string name
void[] m_init
Methods
string toString() const pure
bool opEquals(Object o)
size_t getHash(scope const void * p) const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
void swap(void * p1, void * p2) const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
const(OffsetTypeInfo)[] offTi() const
void destroy(void * p) const
void postblit(void * p) const
const(void)[] initializer() const
size_t talign() @property nothrow pure const
immutable(void) * rtInfo() @property const
Fields
TypeInfo m_next
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
void swap(void * p1, void * p2) const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
Fields
TypeInfo value
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
void swap(void * p1, void * p2) const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
size_t talign() @property nothrow pure const
immutable(void) * rtInfo() @property nothrow pure const @safe
Fields
TypeInfo value
size_t len
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
void swap(void * p1, void * p2) const
const(void)[] initializer() nothrow pure const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
void destroy(void * p) const
void postblit(void * p) const
size_t talign() @property nothrow pure const
immutable(void) * rtInfo() @property nothrow pure const @safe
Fields
TypeInfo value
TypeInfo entry
bool function(scope const void * p1, scope const void * p2) nothrow @safe xopEquals
hash_t function(scope const void *) nothrow @safe xtoHash
Methods
string toString() const
bool opEquals(Object o)
bool equals(in void * p1, in void * p2) @trusted const
hash_t getHash(scope const void * p) nothrow @trusted const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
size_t talign() @property nothrow pure const
Fields
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
void swap(void * p1, void * p2) const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
const(void)[] initializer() nothrow pure const
size_t talign() @property nothrow pure const
Fields
string decoMangled function type string
Methods
string toString() const pure @trusted
bool opEquals(Object o)
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @safe
immutable(void) * rtInfo() @property nothrow pure const @safe
Fields
string deco
Methods
string toString() const pure @trusted
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
uint flags() @property nothrow pure const
size_t talign() @property nothrow pure const
immutable(void) * rtInfo() @property nothrow pure const @safe

Runtime type information about a class. Can be retrieved from an object instance by using the

spec/expression.
Fields
byte[] m_init
string name
void *[] vtbl
Interface[] interfaces
void * destructor
void function(Object) classInvariant
ClassFlags m_flags
ushort depth
void * deallocator
OffsetTypeInfo[] m_offTi
void function(Object) defaultConstructor
immutable(void) * m_RTInfo
uint[4] nameSig
Methods
string toString() const pure
bool opEquals(const TypeInfo o) const
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() nothrow pure const @safe
uint flags() @property nothrow pure const
const(OffsetTypeInfo)[] offTi() @property nothrow pure const
final @property auto info() @safe @nogc nothrow pure const return
final @property auto typeinfo() @safe @nogc nothrow pure const return
immutable(void) * rtInfo() @property const
const(TypeInfo_Class) find(const scope char[] classname)Search all modules for TypeInfo_Class corresponding to classname. Returns: null if not found
Object create() constCreate instance of Object represented by 'this'.
bool isBaseOf(scope const TypeInfo_Class child) const @nogc nothrow pure @trustedReturns true if the class described by `child` derives from or is the class described by this `TypeInfo_Class`. Always returns false if the argument is null.
Nested Templates
ClassFlags
Fields
Methods
string toString() const pure
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
uint flags() @property nothrow pure const
bool isBaseOf(scope const TypeInfo_Class child) const @nogc nothrow pure @trustedReturns true if the class described by `child` derives from the interface described by this `TypeInfo_Interface`. Always returns false if the argument is null.
bool isBaseOf(scope const TypeInfo_Interface child) const @nogc nothrow pure @trustedReturns true if the interface described by `child` derives from or is the interface described by this `TypeInfo_Interface`. Always returns false if the argument is null.
Fields
string mangledName
void[] m_init
size_t function(in void *) xtoHash
bool function(in void *, in void *) xopEquals
int function(in void *, in void *) xopCmp
string function(in void *) xtoString
StructFlags m_flags
void function(void *) xpostblit
uint m_align
immutable(void) * m_RTInfo
Methods
string toString() const
size_t toHash() const
bool opEquals(Object o)
size_t getHash(scope const void * p) @trusted pure nothrow const
bool equals(in void * p1, in void * p2) @trusted pure nothrow const
int compare(in void * p1, in void * p2) @trusted pure nothrow const
size_t tsize() @property nothrow pure const
const(void)[] initializer() nothrow pure const @safe
uint flags() @property nothrow pure const
size_t talign() @property nothrow pure const
void destroy(void * p) const
void postblit(void * p) const
string name() @property nothrow const @trusted
immutable(void) * rtInfo() @property nothrow pure const @safe
Nested Templates
_memberFunc
Fields
TypeInfo[] elements
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
const(void)[] initializer() const @trusted
void swap(void * p1, void * p2) const
void destroy(void * p) const
void postblit(void * p) const
size_t talign() @property nothrow pure const
Fields
Methods
string toString() const
bool opEquals(Object o)
size_t getHash(scope const void * p) const
bool equals(in void * p1, in void * p2) const
int compare(in void * p1, in void * p2) const
size_t tsize() @property nothrow pure const
void swap(void * p1, void * p2) const
inout(TypeInfo) next() @property nothrow pure inout
uint flags() @property nothrow pure const
const(void)[] initializer() nothrow pure const
size_t talign() @property nothrow pure const
Methods
string toString() const
Methods
string toString() const
Methods
string toString() const

An instance of ModuleInfo is generated into the object file for each compiled module.

It provides access to various aspects of the module. It is not generated for betterC.

Fields
uint _flags
uint _index
Methods
private void * addrOf(int flag) return nothrow pure @nogc
uint index() @property nothrow pure @nogc
uint flags() @property nothrow pure @nogc
void function() tlsctor() @property nothrow pure @nogcReturns: module constructor for thread locals, `null` if there isn't one
void function() tlsdtor() @property nothrow pure @nogcReturns: module destructor for thread locals, `null` if there isn't one
void * xgetMembers() @property nothrow pure @nogcReturns: address of a module's `const(MemberInfo)[] getMembers(string)` function, `null` if there isn't one
void function() ctor() @property nothrow pure @nogcReturns: module constructor, `null` if there isn't one
void function() dtor() @property nothrow pure @nogcReturns: module destructor, `null` if there isn't one
void function() ictor() @property nothrow pure @nogcReturns: module order independent constructor, `null` if there isn't one
void function() unitTest() @property nothrow pure @nogcReturns: address of function that runs the module's unittests, `null` if there isn't one
immutable(ModuleInfo *)[] importedModules() @property return nothrow pure @nogcReturns: array of pointers to the ModuleInfo's of modules imported by this one
TypeInfo_Class[] localClasses() @property return nothrow pure @nogcReturns: array of TypeInfo_Class references for classes defined in this module
string name() @property return nothrow pure @nogcReturns: name of module, `null` if no name
int opApply(scope int delegate(ModuleInfo *) dg)
classThrowable : Object

The base class of all thrown objects.

All thrown objects must inherit from Throwable. Class Exception, which derives from this class, represents the category of thrown objects that are safe to catch and handle. In principle, one should not catch Throwable objects that are not derived from Exception, as they represent unrecoverable runtime errors. Certain runtime guarantees may fail to hold when these errors are thrown, making it unsafe to continue execution after catching them.

Fields
string msg
string fileThe file name of the D source code corresponding with where the error was thrown from.
size_t lineThe line number of the D source code corresponding with where the error was thrown from.
TraceInfo infoThe stack trace of where the error happened. This is an opaque object that can either be converted to string, or iterated over with foreach to extract the items in the stack trace (as strings).
TraceDeallocator infoDeallocatorIf set, this is used to deallocate the TraceInfo on destruction.
private void * _nextInChainPtrA reference to the next error in the list. This is used when a new Throwable is thrown from inside a catch block. The originally caught Exception will be chained to the new Throwable via this field.
private uint _refcount
Methods
private bool _nextIsRefcounted() @property @trusted scope pure nothrow @nogc const
inout(Throwable) next() @property @trusted inout return scope pure nothrow @nogcReturns: A reference to the next error in the list. This is used when a new Throwable is thrown from inside a catch block. The originally caught Exception will be chained to the new Throwable via t...
void next(Throwable tail) @property @trusted scope pure nothrow @nogcReplace next in chain with `tail`. Use `chainTogether` instead if at all possible.
uint refcount() @system @nogc pure nothrow ref returnReturns: mutable reference to the reference count, which is 0 - allocated by the GC, 1 - allocated by dnewThrowable(), and >=2 which is the reference count + 1 Note: Marked as `@system` to discoura...
int opApply(scope int delegate(Throwable) dg)Loop over the chain of Throwables.
Throwable chainTogether(return scope Throwable e1, return scope Throwable e2) @__future @system @nogc pure nothrowAppend `e2` to chain of exceptions that starts with `e1`. Params: e1 = start of chain (can be null) e2 = second part of chain (can be null) Returns: Throwable that is at the start of the chain; nul...
string toString()Overrides Object.toString and returns the error message. Internally this forwards to the toString overload that takes a sink delegate.
void toString(scope void delegate(in char[]) sink) constThe Throwable hierarchy uses a toString overload that takes a sink delegate to avoid GC allocations, which cannot be performed in certain error situations. Override this toString method to customi...
const(char)[] message() @__future const @safe nothrowGet the message describing the error.
Constructors
this(string msg, Throwable nextInChain = null)
this(string msg, string file, size_t line, Throwable nextInChain = null)
Destructors
classException : Throwable

The base class of all errors that are safe to catch and handle.

In principle, only thrown objects derived from this class are safe to catch inside a catch block. Thrown objects not derived from Exception represent runtime errors that should not be caught, as certain runtime guarantees may not hold, making it unsafe to continue program execution.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)Creates a new instance of Exception. The nextInChain parameter is used internally and should always be null when passed by user code. This constructor does not automatically throw the newly-created...
this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
classError : Throwable

The base class of all unrecoverable runtime errors.

This represents the category of Throwable objects that are not safe to catch and handle. In principle, one should not catch Error objects, as they represent unrecoverable runtime errors. Certain runtime guarantees may fail to hold when these errors are thrown, making it unsafe to continue execution after catching them.

Fields
Throwable bypassedExceptionThe first Exception which was bypassed when this Error was thrown, or null if no Exceptions were pending.
Constructors
this(string msg, Throwable nextInChain = null)Creates a new instance of Error. The nextInChain parameter is used internally and should always be null when passed by user code. This constructor does not automatically throw the newly-created Err...
this(string msg, string file, size_t line, Throwable nextInChain = null)
aliasAssociativeArray(Key, Value) = Value[Key]
class__cpp_type_info_ptr
Fields
void * ptr

Functions 52

fnbool opEquals(LHS, RHS)(LHS lhs, RHS rhs) if ((is(LHS : const Object) || is(LHS : const shared Object)) && (is(RHS : const Object) || is(RHS : const shared Object)))Implementation for class opEquals override. Calls the class-defined methods after a null check. Please note this is not nogc right now, even if your implementation is, because of the typeinfo name ...
private fnvoid _d_setSameMutex(shared Object ownee, shared Object owner) nothrow;
fnvoid setSameMutex(shared Object ownee, shared Object owner)Makes ownee use owner's mutex. This will initialize owner's mutex if it hasn't been set yet. Params: ownee = object to change owner = source object
private fnObject _d_newclass(const TypeInfo_Class ci)
fnint _d_isbaseof(scope ClassInfo oc, scope const ClassInfo c) @nogc nothrow pure @safe
fnObject _d_toObject(return scope void * p) @nogc nothrow pure @trustedGiven a pointer: If it is an Object, return that Object. If it is an interface, return the Object implementing the interface. If it is null, return null. Else, undefined crash
fnnoreturn _d_arraybounds(string file, uint line) @trusted pure nothrow @nogc
fnvoid * aaLiteral(Key, Value)(Key[] keys, Value[] values)
fnauto _aaAsStruct(K, V)(V[K] aa) @safe
fnvoid clear(Value, Key)(Value[Key] aa) @trustedRemoves all remaining keys and values from an associative array. Params: aa = The associative array.
fnvoid clear(Value, Key)(Value[Key] * aa) @trustedditto
fnValue[Key] rehash(Value, Key)(Value[Key] aa)Reorganizes the associative array in place so that lookups are more efficient. Params: aa = The associative array. Returns: The rehashed associative array. Note: emulated by the compiler during...
fnValue[Key] rehash(T : Value[Key], Value, Key)(T * aa)ditto
fnValue[Key] rehash(T : shared Value[Key], Value, Key)(auto ref T aa)ditto
fnValue[Key] rehash(T : shared Value[Key], Value, Key)(T * aa)ditto
fnauto dup(T : V[K], K, V)(T aa)Creates a new associative array of the same size and copies the contents of the associative array into it. Params: aa = The associative array. Note: emulated by the compiler during CTFE
fnauto dup(T : V[K], K, V)(T * aa)ditto
private fnauto _aaToRange(K, V)(auto ref inout V[K] aa) @trusted
fnauto byKey(T : V[K], K, V)(T aa) pure nothrow @nogc @safeReturns a forward range which will iterate over the keys of the associative array. The keys are returned by reference.
fnauto byKey(K, V)(V[K] * aa) pure nothrow @nogcditto
fnauto byValue(T : V[K], K, V)(T aa) pure nothrow @nogc @safeReturns a forward range which will iterate over the values of the associative array. The values are returned by reference.
fnauto byValue(K, V)(V[K] * aa) pure nothrow @nogcditto
fnauto byKeyValue(T : V[K], K, V)(T aa) pure nothrow @nogc @safeReturns a forward range which will iterate over the key-value pairs of the associative array. The returned pairs are represented by an opaque type with `.key` and `.value` properties for accessing ...
fnauto byKeyValue(T : V[K], K, V)(T * aa) pure nothrow @nogcditto
fnauto keys(Value, Key)(inout Value[Key] aa) @propertyReturns a newly allocated dynamic array containing a copy of the keys from the associative array. Params: aa = The associative array. Returns: A dynamic array containing a copy of the keys. Not...
fnauto keys(T : Value[Key], Value, Key)(T * aa) @propertyditto
fnauto values(Value, Key)(inout Value[Key] aa) @propertyReturns a newly allocated dynamic array containing a copy of the values from the associative array. Params: aa = The associative array. Returns: A dynamic array containing a copy of the values....
fnauto values(T : Value[Key], Value, Key)(T * aa) @propertyditto
fninout(V) get(K, V)(inout(V[K]) aa, K key, lazy inout(V) defaultValue)If `key` is in `aa`, returns corresponding value; otherwise it evaluates and returns `defaultValue`. Params: aa = The associative array. key = The key. defaultValue = The default value. Retu...
fninout(V) get(K, V)(inout(V[K]) * aa, K key, lazy inout(V) defaultValue)ditto
fnV require(K, V)(ref V[K] aa, K key, lazy V value = V.init) refIf `key` is in `aa`, returns corresponding value; otherwise it evaluates `value`, adds it to the associative array and returns it. Params: aa = The associative array. key = The key. value = ...
fnvoid update(K, V, C, U)(ref V[K] aa, K key, scope C create, scope U update) if (is(typeof(create()) : V) && (is(typeof(update(aa[K.init])) : V) || is(typeof(update(aa[K.init])) == void)))Calls `create` if `key` doesn't exist in the associative array, otherwise calls `update`. `create` returns a corresponding value for `key`. `update` accepts a value parameter. If it returns a value...
fnbool _xopEquals(in void *, in void *)
fnbool _xopCmp(in void *, in void *)
private fninout(TypeInfo) getElement(return scope inout TypeInfo value) @trusted pure nothrow
private fnsize_t getArrayHash(const scope TypeInfo element, const scope void * ptr, const size_t count) @trusted nothrow
fn@property auto dup(T)(T[] a) if (!is(const(T) : T))Provide the .dup array property.
fnT[] dup(T)(const(T)[] a) if (is(const(T) : T)) @propertyditto
fnimmutable(T)[] idup(T)(T[] a) @propertyProvide the .idup array property.
fnimmutable(T)[] idup(T: void)(const(T)[] a) @propertyditto
fnsize_t capacity(T)(T[] arr) @property pure nothrow @trusted(Property) Gets the current capacity of a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
fnsize_t reserve(T)(ref T[] arr, size_t newcapacity) pure nothrow @trustedReserves capacity for a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
fninout(T[]) assumeSafeAppend(T)(auto ref inout(T[]) arr) ref nothrow @systemAssume that it is safe to append to this array. Appends made to this array after calling this function may append in place, even if the array was a slice of a larger array to begin with.
private fnvoid _doPostblit(T)(T[] arr)
fnvoid destroy(bool initialize = true, T)(ref T obj) if (is(T == struct))Destroys the given object and optionally resets to initial state. It's used to destroy an object, calling its destructor or finalizer so it no longer references any other objects. It does not initi...
private fnvoid rt_finalize2(void * p, bool det = true, bool resetMemory = true) nothrow;
fnvoid destroy(bool initialize = true, T)(T obj) if (is(T == class))ditto
fnvoid destroy(bool initialize = true, T)(T obj) if (is(T == interface))ditto
fnvoid destroy(bool initialize = true, T)(ref T obj) if (__traits(isStaticArray, T))ditto
fnvoid destroy(bool initialize = true, T)(ref T obj) if (!is(T == struct) && !is(T == interface) && !is(T == class) && !__traits(isStaticArray, T))ditto
fnvoid _d_delThrowable(scope Throwable) @trusted @nogc nothrow pure
fnint __cmp(C1, C2)(C1 lhs, C2 rhs) if ((is(C1 : const(Object)) || (is(C1 == interface) && (__traits(getLinkage, C1) == "D"))) && (is(C2 : const(Object)) || (is(C2 == interface) && (__traits(getLinkage, C2) == "D"))))

Variables 3

private enumvarisSafeCopyable = is(typeof(() @safe { union U { T x; } T * x; auto u = U(* x); }))
enumvarrtinfoNoPointers = null

shortcuts for the precise GC, also generated by the compiler used instead of the actual pointer bitmap

enumvarrtinfoHasPointers = cast(void *) 1

Templates 4

tmplRTInfoImpl(size_t[] pointerBitmap)

Create RTInfo for type T

tmplRTInfo(T)
tmpl_arrayOp(Args...)
tmplimported(string moduleName)

Provides an "inline import", i.e. an import that is only available for a limited lookup. For example:

void fun(imported!"std.stdio".File input)
{
   ... use File from std.stdio normally ...
}

There is no need to import std.stdio at top level, so fun carries its own dependencies. The same approach can be used for template constraints:

void fun(T)(imported!"std.stdio".File input, T value)
if (imported!"std.traits".isIntegral!T)
{
   ...
}

An inline import may be used in conjunction with the with statement as well. Inside the scope controlled by with, all symbols in the imported module are made available:

void fun()
{
   with (imported!"std.datetime")
   with (imported!"std.stdio")
   {
       Clock.currTime.writeln;
   }
}

The advantages of inline imports over top-level uses of the import declaration are the following:

  • The imported template specifies dependencies at declaration level, not at

    module level. This allows reasoning about the dependency cost of declarations in separation instead of aggregated at module level.

  • Declarations using imported are easier to move around because they don't

    require top-level context, making for simpler and quicker refactorings.

  • Declarations using imported scale better with templates. This is because

    templates that are not instantiated do not have their parameters and constraints instantiated, so additional modules are not imported without necessity. This makes the cost of unused templates negligible. Dependencies are pulled on a need basis depending on the declarations used by client code.

The use of imported also has drawbacks:

  • If most declarations in a module need the same imports, then factoring them

    at top level, outside the declarations, is simpler than repeating them.

  • Traditional dependency-tracking tools such as make and other build systems

    assume file-level dependencies and need special tooling (such as rdmd) in order to work efficiently.

  • Dependencies at the top of a module are easier to inspect quickly than

    dependencies spread throughout the module.

See Also

The forum discussion that led to the creation of the imported facility. Credit is

due to Daniel Nielsen and Dominikus Dittes Scherkl.