February 06, 2006
Ivan Senji wrote:
> Chris Sauls wrote:
> 
>> Tyler wrote:
>>
>>> I'm working on converting a program from C to D and one of the data structures
>>> in it could be more easily represented in D as an associative array of arrays of
>>> 2 integers (i.e. "int[char[]][2] example;")  Can D do this?  The latgest DMD
>>> compiler lets me declare it, but when I try to use it ("example["foo"][1] =
>>> 55;") it gives me errors about how it can't implicitly cast char[] to int.
>>>
>>>
>>
>> Its a simple, and common mistake for thsoe new to D arrays.  The basic thing to remember, is that decleration and usage are in reverse syntactic directions, and declerations are "read" from right to left.  In other words:
>>
>> # // declare
>> # int[2][char[]] example;
>> #
>> # // use
>> # example["foo"c][1] = 55;
> 
> 
> Common mistake indeed, but the above doesn't work. Instead you get
> ArrayBoundsError. Why would that be?

The integer array (i.e. the init value of example["foo"]) inits to length 0.  Try this:



int[2][char[]] example;
example["foo"].length = 2;
example["foo"][1] = 55;
February 07, 2006
Russ Lewis wrote:
> The integer array (i.e. the init value of example["foo"]) inits to length 0.  Try this:
> 
> 
> 
> int[2][char[]] example;
> example["foo"].length = 2;

You can't set length of a static array, you get (as expected):

constant example["foo"].length is not an lvalue

Am I the only one that is (again) begining to think that although D does some thing with arrays great for example slicing and ~, that there are very very big flaws?
What I thing D is missing are two types of dynamic arrays:

1. The one we have now:

int[][] bla = new int[2];
bla[0].length = 5;
bla[1].length = 100;

2. Dynamic rectangular arrays:

int[,] bla = new int[7,3]; //for example in C#

Static arrays can be useful sometimes but many many times i don't know the exact size at compile-time but still want a tightly packed multidimensional rectangular array.


> example["foo"][1] = 55;
February 07, 2006
Ivan Senji wrote:
> Russ Lewis wrote:
> 
>> The integer array (i.e. the init value of example["foo"]) inits to length 0.  Try this:
>>
>>
>>
>> int[2][char[]] example;
>> example["foo"].length = 2;
> 
> 
> You can't set length of a static array, you get (as expected):

Sorry, can't believe I overlooked that.
February 08, 2006
On Tue, 07 Feb 2006 14:56:11 +0100, Ivan Senji wrote:


[snip]

> 2. Dynamic rectangular arrays:
> 
> int[,] bla = new int[7,3]; //for example in C#
> 
> Static arrays can be useful sometimes but many many times i don't know the exact size at compile-time but still want a tightly packed multidimensional rectangular array.

In the meantime, you can do this sort of thing ...

template InitRectArray(T)
{
    T[][] InitRectArray(int i, int j)
    {
        T[][] lTemp;
        lTemp.length = i;
        lTemp[] = new T[j];

        return lTemp;
    }
}

import std.stdio;
void main()
{

    int[][] bla;

    bla = InitRectArray!(int)(7,3);
    foreach( int i, int[] a; bla )
    {
        foreach(int j, int b; a)
        {
            writefln(i, "," , j);
        }
    }
}


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
8/02/2006 11:50:53 AM
February 08, 2006
Derek Parnell wrote:
> On Tue, 07 Feb 2006 14:56:11 +0100, Ivan Senji wrote:
> 
> 
> [snip]
>  
> 
>>2. Dynamic rectangular arrays:
>>
>>int[,] bla = new int[7,3]; //for example in C#
>>
>>Static arrays can be useful sometimes but many many times i don't know the exact size at compile-time but still want a tightly packed multidimensional rectangular array.
> 
> 
> In the meantime, you can do this sort of thing ...

Thanks for the answer. Yes i do these sort of things but that is not the point. The point is: arrays are the most basic concept of a language. Far more basic than even classes and delegates, and doing it right in the language is important.

It is funny how you can do all these wonderfull thing with arrays like dynamically resize and slice them, have associative arrays... but also not to be able to use array literals or dynamic rectangular arrays.

> 
> template InitRectArray(T)
> {
>     T[][] InitRectArray(int i, int j)
>     {
>         T[][] lTemp;
>         lTemp.length = i;
>         lTemp[] = new T[j];
> 
>         return lTemp;
>     }
> }
> 
> import std.stdio;
> void main()
> {
> 
>     int[][] bla;
> 
>     bla = InitRectArray!(int)(7,3);
>     foreach( int i, int[] a; bla )
>     {
>         foreach(int j, int b; a)
>         {
>             writefln(i, "," , j);
>         }
>     }
> }
> 

Although this template does allow to emulate the creation of a rectangular-looking array it in fact isn't. Rectangular array must be continuous in memory, not a bunch of references to 1D arrays.
February 09, 2006
Ivan Senji wrote:
> Chris Sauls wrote:
>> I had to think about it for a bit, but I know why... and it dregs up an ancient topic.  It issues the ArrayBoundsError because the key "foo" does not yet exist.  Now, normally an assignment expression creates the key, but apparently when one includes extra dimensions then D doesn't catch this.
>>
>> Bug?  Or annoying feature?
> 
> As you said assignment normally creates the key, so i would vote for bug.
> 
Not a bug. Assignment only creates a key in the form:
  example["foo"] = ...
and not:
  example["foo"][2] = ...
Because the latter is index assignment of the static array, not of the
associative array.

> But the bigger problem is how would one manully create a static array. It just isn't possible.
> 
You could simply try to assign to another static array, but the problem
is that you cannot assign to static arrays, so this will never work:
   example["foo"] = ...



-> What could probably be made, is to not allow creation of types of the
following structure:
   int[2][] example;

That is, one can't create dynamic arrays with static elements.


-- 
Bruno Medeiros - CS/E student
"Certain aspects of D are a pathway to many abilities some consider to
be... unnatural."

February 09, 2006
Bruno Medeiros wrote:
> Ivan Senji wrote:
>> As you said assignment normally creates the key, so i would vote for bug.
>>
> Not a bug. Assignment only creates a key in the form:
>   example["foo"] = ...
> and not:
>   example["foo"][2] = ...
> Because the latter is index assignment of the static array, not of the
> associative array.

I realized that. So there is now solution to the problem of associative array of static arrays.

> 
>> But the bigger problem is how would one manully create a static array. It just isn't possible.
>>
> You could simply try to assign to another static array, but the problem
> is that you cannot assign to static arrays, so this will never work:
>    example["foo"] = ...

That is exactly what my "just isn't possible" meant.

> 
> 
> 
> -> What could probably be made, is to not allow creation of types of the
> following structure:
>    int[2][] example;
> 

NOOO! Are you trying to break my existing code? :)

This works and i use this! There is no reason why this wouldn't work.

> That is, one can't create dynamic arrays with static elements.

Yes, one can, but can't create associative arrays of static arrays.
February 10, 2006
Ivan Senji wrote:
> Bruno Medeiros wrote:
>> Ivan Senji wrote:
>>
>>> But the bigger problem is how would one manully create a static array. It just isn't possible.
>>>
>> You could simply try to assign to another static array, but the problem
>> is that you cannot assign to static arrays, so this will never work:
>>    example["foo"] = ...
> 
> That is exactly what my "just isn't possible" meant.
> 
It won't work because you cannot assign to static arrays, not because it isn't possible to create a static arrays [literal]. (They're different things)

>>
>> -> What could probably be made, is to not allow creation of types of the
>> following structure:
>>    int[2][] example;
>>
> 
> NOOO! Are you trying to break my existing code? :)
> 
> This works and i use this! There is no reason why this wouldn't work.
> 
>> That is, one can't create dynamic arrays with static elements.
> 
> Yes, one can, but can't create associative arrays of static arrays.

Damn, you are correct. It is true my initial assumption that you also can not do this (assign to a static array) with dynamic arrays:
  example[10] = ... ; //(assign to a static array)
So I was thinking that dynamic arrays with static elements wouldn't work too. However, I forgot that in the case of dynamic arrays, one can create elements by changing length. Thus the following is possible:
  example.length = 10
  example[0][] = ... ; //(copy to a static array, that is ok)

-- 
Bruno Medeiros - CS/E student
"Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
1 2 3
Next ›   Last »