$(DDOC $(DDOC_BLANKLINE ) $(DDOC_BLANKLINE ) $(SPEC_S Properties, $(DDOC_BLANKLINE ) $(HEADERNAV_TOC $(HEADERNAV_ITEM init, .init Property) $(HEADERNAV_ITEM stringof, .stringof Property) $(HEADERNAV_ITEM sizeof, .sizeof Property) $(HEADERNAV_ITEM alignof, .alignof Property) $(HEADERNAV_ITEM mangleof, .mangleof Property) $(HEADERNAV_ITEM classinfo, .classinfo Property) $(HEADERNAV_ITEM classproperties, User-Defined Properties) ) $(DDOC_BLANKLINE ) $(P Every symbol, type, and expression has properties that can be queried:) $(DDOC_BLANKLINE ) $(TABLE2 Property Examples, Expression, Value $(TROW $(D int.sizeof), yields 4) $(TROW $(D float.nan), yields the floating point nan (Not A Number) value) $(TROW $(D (float).nan), yields the floating point nan value) $(TROW $(D (3).sizeof), yields 4 (because 3 is an int)) $(DDOC_BLANKLINE ) $(TROW $(D int.init), default initializer for ints) $(TROW $(D int.mangleof), yields the string "i") $(TROW $(D int.stringof), yields the string "int") $(TROW $(D (1+2).stringof), yields the string "1 + 2") ) $(DDOC_BLANKLINE ) $(BR ) $(DDOC_BLANKLINE ) $(TABLE2 Properties for All Types, Property, Description $(TROW $(RELATIVE_LINK2 init, $(D .init)), initializer) $(TROW $(RELATIVE_LINK2 sizeof, $(D .sizeof)), size in bytes) $(TROW $(RELATIVE_LINK2 alignof, $(D .alignof)), alignment size) $(TROW $(RELATIVE_LINK2 mangleof, $(D .mangleof)), string representing the $(SINGLEQUOTE mangled) representation of the type) $(TROW $(RELATIVE_LINK2 stringof, $(D .stringof)), string representing the source representation of the type) ) $(DDOC_BLANKLINE ) $(BR ) $(DDOC_BLANKLINE ) $(TABLE2 Properties for Integral Types, Property, Description $(TROW $(D .init), initializer) $(TROW $(D .max), maximum value) $(TROW $(D .min), minimum value) ) $(DDOC_BLANKLINE ) $(BR ) $(DDOC_BLANKLINE ) $(TABLE_2COLS Properties for Floating Point Types, Property, Description $(TROW $(D .init), initializer (NaN)) $(TROW $(D .infinity), infinity value) $(TROW $(D .nan), NaN value) $(TROW $(D .dig), number of decimal digits of precision) $(TROW $(D .epsilon), smallest increment to the value 1) $(TROW $(D .mant_dig), number of bits in mantissa) $(TROW $(D .max_10_exp), maximum int value such that 10$(SUPERSCRIPT $(D max_10_exp)) is representable) $(TROW $(D .max_exp), maximum int value such that 2$(SUPERSCRIPT $(D max_exp-1)) is representable) $(TROW $(D .min_10_exp), minimum int value such that 10$(SUPERSCRIPT $(D min_10_exp)) is representable as a normalized value) $(TROW $(D .min_exp), minimum int value such that 2$(SUPERSCRIPT $(D min_exp-1)) is representable as a normalized value) $(TROW $(D .max), largest representable value that's not infinity) $(DDOC_BLANKLINE ) $(TROW $(D .min_normal), smallest representable normalized value that's not 0) $(TROW $(D .re), real part) $(TROW $(D .im), imaginary part) ) $(DDOC_BLANKLINE ) $(BR ) $(DDOC_BLANKLINE ) $(TABLE2 Properties for Class Types, Property, Description $(TROW $(RELATIVE_LINK2 classinfo, $(D .classinfo)), Information about the dynamic type of the class) ) $(DDOC_BLANKLINE )

$(LNAME2 init, .init Property)

$(DDOC_BLANKLINE ) $(P $(D .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. ) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD int) a; $(D_KEYWORD int) b = 1; $(D_KEYWORD int).init $(D_COMMENT // is 0 )a.init $(D_COMMENT // is 0 )b.init $(D_COMMENT // is 0 ) $(D_KEYWORD struct) Foo { $(D_KEYWORD int) a; $(D_KEYWORD int) b = 7; } Foo.init.a $(D_COMMENT // is 0 )Foo.init.b $(D_COMMENT // is 7 )) $(DDOC_BLANKLINE ) $(P $(B Note:) $(D .init) produces a default initialized object, not default constructed. If there is a default constructor for an object, it may produce a different value.) $(DDOC_BLANKLINE ) $(OL $(LI If $(D T) is a nested struct, the context pointer in $(D T.init) is $(D null).) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD void) main() { $(D_KEYWORD int) x; $(D_KEYWORD struct) S { $(D_KEYWORD void) foo() { x = 1; } $(D_COMMENT // access x in enclosing scope via context pointer ) } S s1; $(D_COMMENT // OK. S$(LPAREN)$(RPAREN ) correctly initialize its context pointer. ) S s2 = S(); $(D_COMMENT // OK. same as s1 ) S s3 = S.init; $(D_COMMENT // Bad. the context pointer in s3 is null ) s3.foo(); $(D_COMMENT // Access violation )} ) $(DDOC_BLANKLINE ) $(LI If $(D T) is a struct which has $(CODE @disable this();), $(D T.init) might return a logically incorrect object.) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD struct) S { $(D_KEYWORD int) x; @disable $(D_KEYWORD this)(); $(D_KEYWORD this)($(D_KEYWORD int) n) { x = n; } $(D_KEYWORD invariant) { $(D_KEYWORD assert)(x > 0); } $(D_KEYWORD void) check() {} } $(D_KEYWORD void) main() { $(D_COMMENT //S s1; // Error: variable s1 initializer required for type S ) $(D_COMMENT //S s2 = S$(LPAREN)$(RPAREN ); // Error: constructor S.this is not callable ) $(D_COMMENT // because it is annotated with @disable ) S s3 = S.init; $(D_COMMENT // Bad. s3.x == 0, and it violates the invariant of S ) s3.check(); $(D_COMMENT // Assertion failure )} ) ) $(DDOC_BLANKLINE )

$(LNAME2 stringof, .stringof Property)

$(DDOC_BLANKLINE ) $(P $(D .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. ) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD module) test; $(D_KEYWORD import) std.stdio; $(D_KEYWORD struct) Dog { } $(D_KEYWORD enum) Color { Red } $(D_KEYWORD int) i = 4; $(D_KEYWORD void) main() { writeln((1+2).stringof); $(D_COMMENT // "1 + 2" ) writeln(Dog.stringof); $(D_COMMENT // "Dog" ) writeln(test.Dog.stringof); $(D_COMMENT // "Dog" ) writeln($(D_KEYWORD int).stringof); $(D_COMMENT // "int" ) writeln(($(D_KEYWORD int)*[5][]).stringof); $(D_COMMENT // "int*[5][]" ) writeln(Color.Red.stringof); $(D_COMMENT // "Red" ) writeln((5).stringof); $(D_COMMENT // "5" ) writeln((++i).stringof); $(D_COMMENT // "i += 1" ) writeln(i); $(D_COMMENT // 4 )} ) $(DDOC_BLANKLINE ) $(IMPLEMENTATION_DEFINED The string representation for a type or expression can vary.) $(DDOC_BLANKLINE ) $(BEST_PRACTICE Do not use .stringof for code generation. Instead use the $(DDSUBLINK spec/traits, identifier, identifier) trait, or one of the Phobos helper functions such as $(REF fullyQualifiedName, std,traits).) $(DDOC_BLANKLINE )

$(LNAME2 sizeof, .sizeof Property)

$(DDOC_BLANKLINE ) $(P $(CODE e.sizeof) gives the size in bytes of the expression $(D e). ) $(DDOC_BLANKLINE ) $(P When getting the size of a member, it is not necessary for there to be a $(I this) object: ) $(DDOC_BLANKLINE ) $(D_CODE $(D_KEYWORD struct) S { $(D_KEYWORD int) a; $(D_KEYWORD static) $(D_KEYWORD int) foo() { $(D_KEYWORD return) a.sizeof; $(D_COMMENT // returns 4 ) } } $(D_KEYWORD void) test() { $(D_KEYWORD int) x = S.a.sizeof; $(D_COMMENT // sets x to 4 )} ) $(DDOC_BLANKLINE ) $(P $(CODE .sizeof) applied to a class object returns the size of the class reference, not the class instantiation.) $(DDOC_BLANKLINE )

$(LNAME2 alignof, .alignof Property)

$(DDOC_BLANKLINE ) $(P $(CODE .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. ) $(DDOC_BLANKLINE ) $(IMPLEMENTATION_DEFINED the actual aligned size.) $(DDOC_BLANKLINE ) $(BEST_PRACTICE 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 an assert to assure it is correct.) $(DDOC_BLANKLINE )

$(LNAME2 mangleof, .mangleof Property)

$(DDOC_BLANKLINE ) $(P Mangling refers to how a symbol is represented in text form in the generated object file. $(CODE .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 by $(DDSUBLINK spec/abi, name_mangling, Name Mangling). ) $(DDOC_BLANKLINE ) $(IMPLEMENTATION_DEFINED $(OL $(LI whether a leading underscore is added to a symbol) $(LI 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.) ) ) $(DDOC_BLANKLINE )

$(LNAME2 classinfo, .classinfo Property)

$(DDOC_BLANKLINE ) $(P $(CODE .classinfo) provides information about the dynamic type of a class object. It returns a reference to type $(DDLINK phobos/object, TypeInfo_Class, $(D object.TypeInfo_Class)).) $(DDOC_BLANKLINE ) $(P $(CODE .classinfo) applied to an interface gives the information for the interface, not the class it might be an instance of.) $(DDOC_BLANKLINE )

$(LNAME2 classproperties, User-Defined Properties)

$(DDOC_BLANKLINE ) $(P User-defined properties can be created using $(LINK2 function.html#property-functions, Property Functions).) $(DDOC_BLANKLINE ) $(SPEC_SUBNAV_PREV_NEXT type, Types, attribute, Attributes) ) )