Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Language Reference

table of contents

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

Properties

Contents
  1. Common Properties
  2. Type Properties
  3. Numeric Properties
  4. Derived Type Properties
  5. User-Defined Type Properties
  6. .init Property
    1. .init vs Default Construction
  7. .stringof Property
  8. .sizeof Property
  9. .alignof Property
  10. .mangleof Property
  11. .classinfo Property
  12. .tupleof Property
  13. User-Defined Properties

Common Properties

Every symbol, type, and expression has properties that can be queried:

PropertyDescription
.mangleofstring representing the ‘mangled’ representation of the type
.stringofstring representing the source representation of the type
Examples
Expression Value
int.mangleof yields the string "i"
int.stringof yields the string "int"
(1+2).stringof yields the string "1 + 2"

Type Properties

Every type has properties that can be queried. Every expression also has these properties, which are equivalent to the properties of the expression's type. These properties are:

PropertyDescription
.init initializer
.sizeofsize in bytes
.alignofalignment size
Examples
Expression Value
int.init yields 0
int.sizeofyields 4
(3).sizeofyields 4 (because 3 is an int)

Numeric Properties

Properties for Integral Types
PropertyDescription
.maxmaximum value
.minminimum value

Properties for Floating Point Types
PropertyDescription
.infinityinfinity value
.nanNaN -Not a Number value (other NaN values can be produced)
.dignumber of decimal digits of precision
.epsilonsmallest increment to the value 1
.mant_dignumber of bits in mantissa
.max_10_expmaximum int value such that 10max_10_exp is representable
.max_expmaximum int value such that 2max_exp-1 is representable
.min_10_expminimum int value such that 10min_10_exp is representable as a normalized value
.min_expminimum int value such that 2min_exp-1 is representable as a normalized value
.maxlargest representable value that's not infinity
.min_normalsmallest representable normalized value that's not 0
.rereal part
.imimaginary part
Floating Point Examples
Expression Value
float.nan yields the floating point NaN value
(2.5F).nan yields the floating point NaN value

Derived Type Properties

See:

User-Defined Type Properties

See:

.init Property

.init produces a constant expression that is the default initializer. If applied to a type, it is the default initializer for that type. If applied to a variable or field, it is the default initializer for that variable or field's type. The default values for different kinds of types are described below:

Type.init Value
char'\xff'
dchar'\xffff'
wchar'\xffff'
Enumfirst member value
Integers0
Floating PointNaN
Reference Typesnull
Structseachfield's default value
Unionsfirst member value
Rationale: Where possible, the default value for a type is an invalid value.
int a;int b = 1;staticassert(int.init == 0);staticassert(a.init == 0);staticassert(b.init == 0);struct Foo{int a;int b = 7;}staticassert(Foo.init.a == 0);staticassert(Foo.init.b == 7);

.init vs Default Construction

Note that.init produces a default initialized object, not a default constructed one. If there is a default constructor for an object, it may produce a different value.

  1. IfT is a nested struct, the context pointer inT.init isnull.
  2. void main(){int x;struct S    {void foo() { x = 1; }// access x in enclosing scope via context pointer    }    S s1;// OK. S() correctly initialize its context pointer.    S s2 = S();// OK. same as s1    s1.foo();// OK    S s3 = S.init;// Bad. the context pointer in s3 is null    s3.foo();// Access violation}
  3. IfT is a struct which has@disable this();,T.init might return a logically incorrect object.
  4. struct S{int x;    @disablethis();this(int n) { x = n; }invariant {assert(x > 0); }void check() {}}void main(){//S s1;           // Error: variable s1 initializer required for type S//S s2 = S();     // Error: constructor S.this is not callable// because it is annotated with @disable    S s3 = S.init;// Bad. s3.x == 0, and it violates the invariant of S    s3.check();// Assertion failure}

.stringof Property

.stringof produces a constant string that is the source representation of its prefix. If applied to a type, it is the string for that type. If applied to an expression, it is the source representation of that expression. The expression will not be evaluated.

module test;import std.stdio;struct Dog { }enum Color { Red }int i = 4;void main(){    writeln((1+2).stringof);// "1 + 2"    writeln(test.stringof);// "module test"    writeln(Dog.stringof);// "Dog"    writeln(int.stringof);// "int"    writeln((int*[5][]).stringof);// "int*[5][]"    writeln(Color.Red.stringof);// "Red"    writeln((5).stringof);// "5"    writeln((++i).stringof);// "i += 1"assert(i == 4);// `++i` was not evaluated}
Implementation Defined: The string representation for a type or expression can vary.
Best Practices: Do not use.stringof for code generation. Instead use theidentifier trait, or one of the Phobos helper functions such asstd.traits.fullyQualifiedName.

.sizeof Property

e.sizeof gives the size in bytes of the expressione.

When getting the size of a member, it is not necessary for there to be athis object:

struct S{int a;}staticassert(S.a.sizeof == 4);staticassert(Object.sizeof == (void*).sizeof);

.sizeof applied to a class object returns the size of the class reference, not the class instantiation.

See also:__traits(classInstanceSize).

.alignof Property

.alignof gives the aligned size of an expression or type. For example, an aligned size of 1 means that it is aligned on a byte boundary, 4 means it is aligned on a 32 bit boundary.

Seethealign attribute for an example.

Implementation Defined: the actual aligned size.
Best Practices: Be particularly careful when laying out an object that must line up with an externally imposed layout. Data misalignment can result in particularly pernicious bugs. It's often worth putting in anassert to assure it is correct.

.mangleof Property

Mangling refers to how a symbol is represented in text form in the generated object file..mangleof returns a string literal of the representation of the type or symbol it is applied to. The mangling of types and symbols with D linkage is defined byName Mangling.

Implementation Defined:
  1. whether a leading underscore is added to a symbol
  2. the mangling of types and symbols with non-D linkage. For C and C++ linkage, this will typically match what the associated C or C++ compiler does.

.classinfo Property

.classinfo provides information about the dynamic type of a classobject. It returns a reference to typeobject.TypeInfo_Class.

.classinfo applied to an interface gives the information for theinterface, not the class it might be an instance of.

.tupleof Property

.tupleof provides a symbol sequence of all the non-static fields in a struct or class.The order of the fields in the tuple matches the order in which the fields are declared.

For classes, hidden fields and fields of the bass class are excluded.
A common use case is to iterate over the fields with aforeach loop.

struct Vector3{int x, y, z;}void test(){    Vector3 v = Vector3(5, 10, 15);float sum = 0;foreach(num; v.tupleof)    {        sum+= num;    }    writeln(sum);//30}

User-Defined Properties

User-defined properties can be created usingProperty Functions.

Types
Attributes
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 00:13:44 2026

[8]ページ先頭

©2009-2026 Movatter.jp