File: d.d

package info (click to toggle)
wxwidgets2.8 2.8.7.1-1.1%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 226,072 kB
  • ctags: 277,896
  • sloc: cpp: 1,769,805; xml: 396,717; python: 234,264; ansic: 126,047; makefile: 49,752; sh: 14,235; asm: 284; sql: 263; lex: 194; perl: 139; yacc: 128; pascal: 95; php: 23; haskell: 20; ruby: 20; java: 18; erlang: 17; lisp: 13; tcl: 10; csh: 9; ml: 9; ada: 5
file content (139 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/dmd -run
// Syntax Highlighting test file for D programming language
// A single line comment
/* A multi-line comment about this file.
 * Adopted from http://en.wikipedia.org/wiki/D_%28programming_language%29
 */
 
import std.stdio;
int main(char[][] args)                 
{
    writefln("Hello World");
 
    // Strings are denoted as a dynamic array of chars 'char[]'
    // auto type inference and built-in foreach
    foreach(argc, argv; args)
    {
        auto cl = new CmdLin(argc, argv);    // OOP is supported
        writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv);  // user-defined class properties.
 
        delete cl;   // Garbage Collection or explicit memory management - your choice
    }
 
    // Nested structs, classes and functions
    struct specs
    {
        // all vars automatically initialized to 0 at runtime
        int count, allocated;
        // however you can choose to avoid array initialization
        int[10000] bigarray = void;
    }
 
    specs argspecs(char[][] args)
    // Optional (built-in) function contracts.
    in
    {
        assert(args.length > 0);                   // assert built in
    }
    out(result)
    {
        assert(result.count == CmdLin.total);
        assert(result.allocated > 0);
    }
    body
    {
        specs* s = new specs;
        // no need for '->'
        s.count = args.length;  // The 'length' property is number of elements.
        s.allocated = typeof(args).sizeof; // built-in properties for native types
        foreach(arg; args)
            s.allocated += arg.length * typeof(arg[0]).sizeof;
        return *s;
    }
 
    // built-in string and common string operations, eg. '~' is concatenate.
    char[] argcmsg  = "argc = %d";
    char[] allocmsg = "allocated = %d";
    writefln(argcmsg ~ ", " ~ allocmsg,
            argspecs(args).count,argspecs(args).allocated);
    return 0;
}
 
/**
 * Stores a single command line argument.
 */
class CmdLin
{
    private
    {
        int _argc;
        char[] _argv;
        static uint _totalc;
    }
 
    public:
        /**
         * Object constructor.
         * params:
         *   argc = ordinal count of this argument.
         *   argv = text of the parameter
         */
        this(int argc, char[] argv)
        {
            _argc = argc + 1;
            _argv = argv;
            _totalc++;
        }
 
        ~this() // Object destructor
        {
            // Doesn't actually do anything for this example.
        }
 
        int argnum() // A property that returns arg number
        {
            return _argc;
        }
 
        char[] argv() // A property that returns arg text
        {
            return _argv;
        }
 
        wchar[] suffix() // A property that returns ordinal suffix
        {
            wchar[] suffix; // Built in Unicode strings (UTF-8, UTF-16, UTF-32)
            switch(_argc)
            {
                case 1:
                    suffix = "st";
                    break;
                case 2:
                    suffix = "nd";
                    break;
                case 3:
                    suffix = "rd";
                    break;
                default:  // 'default' is mandatory with "-w" compile switch.
                    suffix = "th";
            }
            return suffix;
        }
 
        /**
          * A static property, as in C++ or Java,
          * applying to the class object rather than instances.
          * returns: The total number of commandline args added.
          */
        static typeof(_totalc) total()
        {
            return _totalc;
        }
 
        // Class invariant, things that must be true after any method is run.
        invariant ()
        {
            assert(_argc > 0);
            assert(_totalc >= _argc);
        }
}