The D Programming Language
by Walter Bright

Listing One

(a) 
static int bar();       // forward reference
int foo()
{
    return bar();
}
static int bar()
{
    ...
}


(b) 
int foo()
{
    return bar();
}
static int bar()
{
    ...
}

Listing Two

(a)
class Foo;
class Bar { Foo f; }
class Foo { int x; }
        

(b)
class Bar { Foo f; }
class Foo { int x; }

Listing Three 

(a)
class Foo
{
    int member();
};

int Foo::member()
{
    ...
}
        

(b)
class Foo
{
    int member();
    {
        ...
    }
};

Listing Four

int i;
Symbol s = null;

for (i = 0; i < 100; i++)
{
    s = search(array[i]);
    if (s != null)
        break;
}
assert(s != null);      // we should have found the Symbol

Listing Five

(a)
class Year
{
    int year;   // years in A.D.
}


(b)
class Year
{
    int year;   // years in A.D.

    invariant
    {
        // Our business app doesn't care about ancient history
        assert(year >= 1900 && year < 3000);
    }
}


Listing Six

(a)
byte *memcpy(byte *to, byte *from, unsigned nbytes)
{
    while (nbytes--)
        to[nbytes] = from[nbytes];
    return to;
}


(b) 
byte *memcpy(byte *to, byte *from, unsigned nbytes)
    in
    {
        assert(to + nbytes < from || from + nbytes < to);
    }
    body
    {
        while (nbytes--)
            to[nbytes] = from[nbytes];
        return to;
    }
        

Listing Seven 

byte *memcpy(byte *to, byte *from, unsigned nbytes)
    in
    {
        assert(to + nbytes < from || from + nbytes < to);
    }
    out(result)
    {
        assert(result == to);
        for (unsigned u = 0; u < nbytes; u++)
            assert(to[u] == from[u]);
    }
    body
    {
        while (nbytes--)
            to[nbytes] = from[nbytes];
        return to;
    }
        

Listing Eight

(a)
class Year
{
    int year;   // years in A.D.

    void setYear(int y)
        in { assert(y >= 0 && y <= 99); }
        body { year = y + 1900; }

    invariant { assert(year >= 1900 && year < 3000); }
}
        
(b)
class FullYear : Year
{
    void setYear(int y)
        in { assert(y >= 1900 && y <= 3000); }
        body { year = y; }
}

(c) 
class FullYear : Year
{
    void setYear(int y)
        in { assert(y >= 1900 && y <= 3000); }
        body { year = (y <= 99) ? y + 1900 : y; }
}






3

