Properties - D Programming Language 2.0 - Digital Mars

D Programming Language 2.0

- Wiki
- Search
- Downloads
- Home

Last update Tue Jun 30 11:31:47 2009

# Properties

Every type and expression has properties that can be queried:

Property Examples
Expression | Value
--- | ---
int.sizeof | yields 4
float.nan | yields the floating point nan (Not A Number) value
(float).nan | yields the floating point nan value
(3).sizeof | yields 4 (because 3 is an int)
2.sizeof | syntax error, since "2." is a floating point number
int.init | default initializer for int's
int.mangleof | yields the string "i"
int.stringof | yields the string "int"
(1+2).stringof | yields the string "1 + 2"

Properties for All Types
Property | Description
--- | ---
.init | initializer
.sizeof | size in bytes (equivalent to C's sizeof(type))
.alignof | alignment size
.mangleof | string representing the 'mangled' representation of the type
.stringof | string representing the source representation of the type

Properties for Integral Types
Property | Description
--- | ---
.init | initializer (0)
.max | maximum value
.min | minimum value

Properties for Floating Point Types
Property | Description
--- | ---
.init | initializer (NaN)
.infinity | infinity value
.nan | NaN value
.dig | number of decimal digits of precision
.epsilon | smallest increment to the value 1
.mant_dig | number of bits in mantissa
.max_10_exp | maximum int value such that 10max_10_exp is representable
.max_exp | maximum int value such that 2max_exp-1 is representable
.min_10_exp | minimum int value such that 10min_10_exp is representable as a normalized value
.min_exp | minimum int value such that 2min_exp-1 is representable as a normalized value
.max | largest representable value that's not infinity
.min | smallest representable normalized value that's not 0
.re | real part
.im | imaginary part

## .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. For example:

    int a;
    int b = 1;
    typedef int t = 2;
    t c;
    t d = cast(t)3;

    int.init	// is 0
    a.init		// is 0
    b.init		// is 0
    t.init		// is 2
    c.init		// is 2
    d.init		// is 2

    struct Foo
    {
        int a;
        int b = 7;
    }

    Foo.a.init	// is 0
    Foo.b.init	// is 7

## .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. Semantic analysis is not done for that expression. For example:

    struct Foo { }

    enum Enum { RED }

    typedef int myint;

    void main()
    {
        writefln((1+2).stringof);       // "1 + 2"
        writefln(Foo.stringof);         // "Foo"
        writefln(test.Foo.stringof);    // "test.Foo"
        writefln(int.stringof);         // "int"
        writefln((int*[5][]).stringof); // "int*[5][]"
        writefln(Enum.RED.stringof);    // "Enum.RED"
        writefln(test.myint.stringof);  // "test.myint"
        writefln((5).stringof);         // "5"
    }

### Class and Struct Properties

Properties are member functions that can be syntactically treated as if they were fields. Properties can be read from or written to. A property is read by calling a method with no arguments; a property is written by calling a method with its argument being the value it is set to.

A simple property would be:

    struct Foo
    {
        int data() { return m_data; }	// read property

        int data(int value) { return m_data = value; } // write property

      private:
        int m_data;
    }

To use it:

    int test()
    {
        Foo f;

        f.data = 3;		// same as f.data(3);
        return f.data + 3;	// same as return f.data() + 3;
    }

The absence of a read method means that the property is write-only. The absence of a write method means that the property is read-only. Multiple write methods can exist; the correct one is selected using the usual function overloading rules.

In all the other respects, these methods are like any other methods. They can be static, have different linkages, be overloaded with methods with multiple parameters, have their address taken, etc.

Note: Properties currently cannot be the lvalue of an op=, ++, or -- operator.

## .sizeof Property

`e.sizeof` gives the size in bytes of the expression e.

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

    struct S {
        int a;
        static int foo() {
    	return a.sizeof;   // returns 4
        }
    }

    void test() {
        int x = S.a.sizeof;    // sets x to 4
    }

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

## .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.

Copyright © 1999-2009 by Digital Mars ®, All Rights Reserved | Page generated by Ddoc. | Comments