http://d.puremagic.com/issues/show_bug.cgi?id=4551

           Summary: D2 Language Docs:
                    http://www.digitalmars.com/d/2.0/arrays.html
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: www.digitalmars.com
        AssignedTo: nob...@puremagic.com
        ReportedBy: andrej.mitrov...@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrov...@gmail.com> 2010-08-01 
09:23:10 PDT ---
"Usage"

In the code example, the lines:

p = s;        // p points to the first element of the array s.
p = a;        // p points to the first element of the array a.

Give compiler errors:

Error: cannot implicitly convert expression (s) of
type int[3u] to int*
Error: cannot implicitly convert expression (a) of
type int[] to int*

These should probably be replaced with:

p = &s[0];        // p points to the first element of the array s.
p = &a[0];        // p points to the first element of the array a.



"Array Properties"

In the first code example there are missing declarations. 
Add these before the first line:

int* p;
int[3] s;
int[] a;



"Setting Dynamic Array Length"

>From the comments in the first example, it states:
    ''// always resized in place because it is sliced
      // from a[] which has enough memory for 15 chars''

But I am not seeing this behavior. I've added two asserts, showing that
manipulating the contents of b[] after it was resized does not affect a[] or
c[]:

void main() {
    char[] a = new char[20];
    char[] b = a[0..10];
    char[] c = a[10..20];

    b.length = 15;    // always resized in place because it is sliced
                      // from a[] which has enough memory for 15 chars
    b[11] = 'x';

    assert(b[11] != a[11]);     // a[11] is not affected
    assert(b[11] != c[1]);      // c[1] is not affected
}



"Functions as Array Properties"

The body of foo() in the example code is not defined and we get the usual
linker nonsense. 
Replace the example code with the following:

void foo(int[] a, int x) { };

void main() {
    int[] array;
    foo(array, 3);
    array.foo(3);    // means the same thing
}



"Array Bounds Checking"

In the example code, "ArrayBoundsError" should be replaced 
with "RangeError"



"Static Initialization of Static Arrays"

The documentation states:
"These arrays are static when they appear in global scope. Otherwise, they need
to be marked with const or static storage classes to make them static arrays."

In non-global scope, I can only compile if the array is marked with static or
static const.

const alone will not compile for me:

enum Color { red, blue, green };

void main() { 
    const int value[Color.max + 1] = [ Color.blue:6, Color.green:2, Color.red:5
]; 
}

test2.d(192): Error: Integer constant expression expected instead of Color.blue
test2.d(192): Error: Integer constant expression expected instead of
Color.green
test2.d(192): Error: Integer constant expression expected instead of Color.red
test2.d(192): Error: Integer constant expression expected instead of Color.blue
test2.d(192): Error: Integer constant expression expected instead of
Color.green
test2.d(192): Error: Integer constant expression expected instead of Color.red



"Special Array Types"

The 6th code example from the title:
    cast(wchar [])"abc"    // this is an array of wchar characters
    "abc"w              // so is this

There's a difference. The first is a mutable wchar[] type, while the
second is immutable(wchar)[] type.

The next example has this line:
    w = \r[0];        // w is assigned the carriage return wchar character

Which errors out:
test2.d(200): Escape String literal \r is deprecated, use double quoted string
literal "\r" instead

Replace that line with:
w = "\r"[0];    // w is assigned the carriage return wchar character



"Using Classes as the KeyType"
The example code mixes "f" and "foo", there is only an "f" identifier so 
it doesn't compile.

But I would replace f with foo anyways, since it makes the example more
readable.
The compilable code is then:

class Foo
{
    int a, b;

    hash_t toHash() { return a + b; }

    bool opEquals(Object o)
    {    
        Foo foo = cast(Foo) o;
        return foo && a == foo.a && b == foo.b;
    }

    int opCmp(Object o)
    {    
        Foo foo = cast(Foo) o;
        if (!foo)
            return -1;
        if (a == foo.a)
            return b - foo.b;
        return a - foo.a;
    }
}



"Using Structs or Unions as the KeyType"

This line in the code example:
foreach (char c; s)

Should be replaced with:
foreach (char c; str)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to