Forms the symbols available to all D programs. Includes Object, which is the root of the class object hierarchy. This module is implicitly imported.
Recursively calls the opPostMove
callbacks of a struct and its members if they're defined.
When moving a struct instance, the compiler emits a call to this function after blitting the instance and before releasing the original instance's memory.
S newLocation
| reference to struct instance being moved into |
S oldLocation
| reference to the original instance |
nothrow
to prevent opPostMove
from being defined without nothrow
, which would allow for possibly confusing changes in program flow.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 initiate a GC cycle or free any GC memory. If initialize
is supplied false
, the object is considered invalid after destruction, and should not be referenced.
class C { struct Agg { static int dtorCount; int x = 10; ~this() { dtorCount++; } } static int dtorCount; string s = "S"; Agg a; ~this() { dtorCount++; } } C c = new C(); assert(c.dtorCount == 0); // destructor not yet called assert(c.s == "S"); // initial state `c.s` is `"S"` assert(c.a.dtorCount == 0); // destructor not yet called assert(c.a.x == 10); // initial state `c.a.x` is `10` c.s = "T"; c.a.x = 30; assert(c.s == "T"); // `c.s` is `"T"` destroy(c); assert(c.dtorCount == 1); // `c`'s destructor was called assert(c.s == "S"); // `c.s` is back to its inital state, `"S"` assert(c.a.dtorCount == 1); // `c.a`'s destructor was called assert(c.a.x == 10); // `c.a.x` is back to its inital state, `10` // check C++ classes work too! extern (C++) class CPP { struct Agg { __gshared int dtorCount; int x = 10; ~this() { dtorCount++; } } __gshared int dtorCount; string s = "S"; Agg a; ~this() { dtorCount++; } } CPP cpp = new CPP(); assert(cpp.dtorCount == 0); // destructor not yet called assert(cpp.s == "S"); // initial state `cpp.s` is `"S"` assert(cpp.a.dtorCount == 0); // destructor not yet called assert(cpp.a.x == 10); // initial state `cpp.a.x` is `10` cpp.s = "T"; cpp.a.x = 30; assert(cpp.s == "T"); // `cpp.s` is `"T"` destroy!false(cpp); // destroy without initialization assert(cpp.dtorCount == 1); // `cpp`'s destructor was called assert(cpp.s == "T"); // `cpp.s` is not initialized assert(cpp.a.dtorCount == 1); // `cpp.a`'s destructor was called assert(cpp.a.x == 30); // `cpp.a.x` is not initialized destroy(cpp); assert(cpp.dtorCount == 2); // `cpp`'s destructor was called again assert(cpp.s == "S"); // `cpp.s` is back to its inital state, `"S"` assert(cpp.a.dtorCount == 2); // `cpp.a`'s destructor was called again assert(cpp.a.x == 10); // `cpp.a.x` is back to its inital state, `10`
int i; assert(i == 0); // `i`'s initial state is `0` i = 1; assert(i == 1); // `i` changed to `1` destroy!false(i); assert(i == 1); // `i` was not initialized destroy(i); assert(i == 0); // `i` is back to its initial state `0`
All D class objects inherit from Object.
Convert Object to a human readable string.
Compute hash function for Object.
Compare with another Object obj.
this < obj | < 0 |
this == obj | 0 |
this > obj | > 0 |
Test whether this
is equal to o
. The default implementation only compares by identity (using the is
operator). Generally, overrides for opEquals
should attempt to compare objects by their contents.
Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor.
module foo.bar; class C { this() { x = 10; } int x; } void main() { auto c = cast(C)Object.factory("foo.bar.C"); assert(c !is null && c.x == 10); }
Returns true if lhs and rhs are equal.
class F { int flag; this(int flag) { this.flag = flag; } } F f; assert(f == f); // both null f = new F(1); assert(f == f); // both aliased to the same object
class F { int flag; this(int flag) { this.flag = flag; } } F f; assert(!(new F(0) == f)); assert(!(f == new F(0)));
class F { int flag; this(int flag) { this.flag = flag; } override bool opEquals(const Object o) { return flag == (cast(F) o).flag; } } F f; assert(new F(0) == new F(0)); assert(!(new F(0) == new F(1)));
int fEquals, gEquals; class Base { int flag; this(int flag) { this.flag = flag; } } class F : Base { this(int flag) { super(flag); } override bool opEquals(const Object o) { fEquals++; return flag == (cast(Base) o).flag; } } class G : Base { this(int flag) { super(flag); } override bool opEquals(const Object o) { gEquals++; return flag == (cast(Base) o).flag; } } assert(new F(1) == new G(1)); assert(fEquals == 1); assert(gEquals == 1);
Information about an interface. When an object is accessed via an interface, an Interface* appears as the first entry in its vtbl.
.classinfo for this interface (not for containing class)
offset to Interface 'this' from Object 'this'
Array of pairs giving the offset and type information for each member in an aggregate.
Offset of member from start of object
TypeInfo for this member
Runtime type information about a type. Can be retrieved for any type using a TypeidExpression.
Computes a hash of the instance of a type.
void* p
| pointer to start of instance of the type |
Compares two instances for equality.
Compares two instances for <, ==, or >.
Returns size of the type.
Swaps two instances of the type.
Get TypeInfo for 'next' type, as defined by what kind of type this is, null if none.
Return 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 default initializer for a single element of the array, use tsize
to get the correct size.
Get flags for type: 1 means GC should scan for pointers, 2 means arg of this type is passed in XMM register
Get type information on the contents of the type; null if not available
Run the destructor on the object and all its sub-objects
Run the postblit on the object and all its sub-objects
Return alignment of type
Return internal info on arguments fitting into 8byte. See X86-64 ABI 3.2.3
Return info used by the garbage collector to do precise collection.
Runtime type information about a class. Can be retrieved from an object instance by using the .classinfo property.
class static initializer (init.length gives size in bytes of class)
class name
virtual function pointer table
interfaces this class implements
base class
Search all modules for TypeInfo_Class corresponding to classname.
Create instance of Object represented by 'this'.
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.
null
if there isn't onenull
if there isn't oneconst(MemberInfo)[] getMembers(string)
function, null
if there isn't onenull
if there isn't onenull
if there isn't onenull
if there isn't onenull
if there isn't onenull
if no nameThe 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.
A message describing the error.
The file name of the D source code corresponding with where the error was thrown from.
The line number of the D source code corresponding with where the error was thrown from.
The 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).
Throwable
is thrown from inside a catch
block. The originally caught Exception
will be chained to the new Throwable
via this field.Replace next in chain with tail
. Use chainTogether
instead if at all possible.
Loop over the chain of Throwables.
Append e2
to chain of exceptions that starts with e1
.
Throwable e1
| start of chain (can be null) |
Throwable e2
| second part of chain (can be null) |
e1
and e2
are nullOverrides Object.toString
and returns the error message. Internally this forwards to the toString
overload that takes a sink delegate.
The 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 customize the error message.
Get the message describing the error. Base behavior is to return the Throwable.msg
field. Override to return some other error message.
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.
bool gotCaught; try { throw new Exception("msg"); } catch (Exception e) { gotCaught = true; assert(e.msg == "msg"); } assert(gotCaught);
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 Exception; the throw
statement should be used for that purpose.
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.
bool gotCaught; try { throw new Error("msg"); } catch (Error e) { gotCaught = true; assert(e.msg == "msg"); } assert(gotCaught);
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 Error; the throw
statement should be used for that purpose.
The first Exception
which was bypassed when this Error was thrown, or null
if no Exception
s were pending.
Removes all remaining keys and values from an associative array.
T aa
| The associative array. |
Reorganizes the associative array in place so that lookups are more efficient.
T aa
| The associative array. |
Create a new associative array of the same size and copy the contents of the associative array into it.
T aa
| The associative array. |
Returns a forward range over the keys of the associative array.
T aa
| The associative array. |
Returns a forward range over the values of the associative array.
T aa
| The associative array. |
Returns a forward range over the key value pairs of the associative array.
T aa
| The associative array. |
Returns a dynamic array, the elements of which are the keys in the associative array.
T aa
| The associative array. |
Returns a dynamic array, the elements of which are the values in the associative array.
T aa
| The associative array. |
Looks up key; if it exists returns corresponding value else evaluates and returns defaultValue.
inout(V[K]) aa
| The associative array. |
K key
| The key. |
inout(V) defaultValue
| The default value. |
Looks up key; if it exists returns corresponding value else evaluates value, adds it to the associative array and returns it.
V[K] aa
| The associative array. |
K key
| The key. |
V value
| The required value. |
auto aa = ["k1": 1]; assert(aa.require("k1", 0) == 1); assert(aa.require("k2", 0) == 0); assert(aa["k2"] == 0);
Looks up key; if it exists applies the update callable else evaluates the create callable and adds it to the associative array
V[K] aa
| The associative array. |
K key
| The key. |
C create
| The callable to apply on create. |
U update
| The callable to apply on update. |
auto aa = ["k1": 1]; aa.update("k1", { return -1; // create (won't be executed }, (ref int v) { return v + 1; // update }); assert(aa["k1"] == 2); aa.update("k2", { return 0; // create }, (ref int v) { return -1; // update (won't be executed) }); assert(aa["k2"] == 0);
Calculates the hash value of arg
with an optional seed
initial value. The result might not be equal to typeid(T).getHash(&arg)
.
T arg
| argument to calculate the hash value of |
size_t seed
| optional seed value (may be used for hash chaining) |
arg
class MyObject { size_t myMegaHash() const @safe pure nothrow { return 42; } } struct Test { int a; string b; MyObject c; size_t toHash() const pure nothrow { size_t hash = a.hashOf(); hash = b.hashOf(hash); size_t h1 = c.myMegaHash(); hash = h1.hashOf(hash); //Mix two hash values return hash; } }
Create RTInfo for type T
shortcuts for the precise GC, also generated by the compiler used instead of the actual pointer bitmap
Provide the .dup array property.
auto arr = [1, 2]; auto arr2 = arr.dup; arr[0] = 0; assert(arr == [0, 2]); assert(arr2 == [1, 2]);
Provide the .idup array property.
char[] arr = ['a', 'b', 'c']; string s = arr.idup; arr[0] = '.'; assert(s == "abc");
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/object.html