
Data Structure

This illustrates a data structure, a means of storing data within a program.

You may see other such structures in the Data Structures category.


Task

Create a compound data type:

  Point(x,y) 



A compound data type is one that holds multiple independent values.



Related task

   Enumeration


See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


void main() {
    // A normal POD struct
    // (if it's nested and it's not static then it has a hidden
    // field that points to the enclosing function):
    static struct Point {
        int x, y;
    }
 
    auto p1 = Point(10, 20);
 
    // It can also be parametrized on the coordinate type:
    static struct Pair(T) {
        T x, y;
    }
 
    // A pair with integer coordinates:
    auto p2 = Pair!int(3, 5);
 
    // A pair with floating point coordinates:
    auto p3 = Pair!double(3, 5);
 
    // Classes (static inner):
    static class PointClass {
        int x, y;
        this(int x_, int y_) {
            this.x = x_;
            this.y = y_;
        }
    }
 
    auto p4 = new PointClass(1, 2);
 
    // There are also library-defined tuples:
    import std.typecons;
 
    alias Tuple!(int,"x", int,"y") PointXY;
 
    auto p5 = PointXY(3, 5);
 
    // And even built-in "type tuples":
    import std.typetuple;
 
    alias TypeTuple!(int, 5) p6;
 
    static assert(is(p6[0] == int));
    static assert(p6[1] == 5);
}