www.digitalmars.com         C & C++   DMDScript  

D - bah!

reply "Andrew Nguyen" <pythonnet hotmail.com> writes:
I say that arrays should have the []'s at the end of the name. Just like
functions. You see, what I see in the C/C++ languages is that the return or
data type is at the begiining while any "grouping" symbols are at the end.

For Example,

in C/C++,
int myarray[]; // The array identifier at end
int myfunc(); // Parens denoting a function

Don't get me wrong, I KNOW that you are supposed to have it at the end
during normal use.

Also, [][]?
Why not seperate the dimensions with commas? IE,

int my_array[56,23]; /*56*23 array */

just like in functions? You dont see
int my_func(6)("3ghfogho");
do you?
I figure that the co-ordinates are essentialy parameters.
You see, you can also use an array as a function! (Though limited, but
follow along)

Let's say I want a function to compare a variable, foo to a constant qux. It
returns 1 if it's higher and 0 if it's lower.

Let's look at this in the ways I've described above:

int qux = 10;

int comparefoo(int foo)
{
 if (foo<qux)
    return 1;
 else(foo=>qux)
    return 0;
}

Now, as an array:

int myarray[1] = {0,1};

Yes, I know, the above wasn't proper D (I still think Mars is cooler) but
still. You get my point?
Jul 19 2002
next sibling parent "anderson" <anderson firestar.com.au> writes:
"Andrew Nguyen" <pythonnet hotmail.com> wrote in message
news:aharap$vh2$1 digitaldaemon.com...
 I say that arrays should have the []'s at the end of the name. Just like
 functions. You see, what I see in the C/C++ languages is that the return

 data type is at the begiining while any "grouping" symbols are at the end.

 For Example,

 in C/C++,
 int myarray[]; // The array identifier at end
 int myfunc(); // Parens denoting a function

D allows for that (although It's not mandatory).
 Don't get me wrong, I KNOW that you are supposed to have it at the end
 during normal use.

 Also, [][]?
 Why not seperate the dimensions with commas? IE,

 int my_array[56,23]; /*56*23 array */

That's a good idea.
 just like in functions? You dont see
 int my_func(6)("3ghfogho");
 do you?
 I figure that the co-ordinates are essentialy parameters.
 You see, you can also use an array as a function! (Though limited, but
 follow along)

 Let's say I want a function to compare a variable, foo to a constant qux.

 returns 1 if it's higher and 0 if it's lower.

 Let's look at this in the ways I've described above:

 int qux = 10;

 int comparefoo(int foo)
 {
  if (foo<qux)
     return 1;
  else(foo=>qux)
     return 0;
 }

Sorry I don't understand? D syntax... int qux = 10; int comparefoo(int foo) { if (foo<qux) return 1; else return 0; } How about... int result = cast(int)(foo<qux);
 Now, as an array:

 int myarray[1] = {0,1};

 Yes, I know, the above wasn't proper D (I still think Mars is cooler) but
 still. You get my point?

Is that ment to be a lookup table (LUT)? How would that work with qux = 10? Or are these examples unrealted?
Jul 20 2002
prev sibling next sibling parent reply C.R.Chafer <blackmarlin nospam.asean-mail.com> writes:
Andrew Nguyen wrote:

 I say that arrays should have the []'s at the end of the name. Just like
 functions. You see, what I see in the C/C++ languages is that the return
 or data type is at the begiining while any "grouping" symbols are at the
 end.
 
 For Example,
 
 in C/C++,
 int myarray[]; // The array identifier at end
 int myfunc(); // Parens denoting a function

You can do this in D - int[] myArray; is not a mandatory format, although I personally find it easier to read (given the choice though I would rather have 'myArray int[];' [as this makes searching long lists of variable declarations easier] - but no language is perfect). At least the D format is consistant if you read it right ... int[] myArray; // accessing myArray returns int[] int myFunc() ... // accessing myFunc returns int int[] myFunc2() ... // accessing myFunc2 returns int[]
 Don't get me wrong, I KNOW that you are supposed to have it at the end
 during normal use.
 
 Also, [][]?
 Why not seperate the dimensions with commas? IE,

[][] is used to designate a ragged array. [,] could be implemented for rectangular arrays. so. int[4][7] array1; // is 4 ptrs to arrays of int length 7 named array1 // (accesses required at least 1 pointer dereference) int[4,7] array2; // is array of width 4, length 7 named array2 // (accesses do not require a pointer dereference - // but do required a multiply / shift and add - though // these may be optimised away some times) If the latter is not currently supported I believe it soon should be (v2?). Also remember we could say x = array1[2].length; How would we do this with your syntax array in a consistant way? x = myarray.length[2]; // only (nice) way I can think of (==7) x = myarray.length; // this would return 4 = 1st parameter // other wise we just have to be inconsistant For a rectangular array the dimensions are fixed on initialisation so we could use x = array2.dimension[0]; // return length == 4 x = array2.dimension[1]; // return width == 7 x = array2.dimension(1); // alt syntax - function style (?) x = array2.length; // error "rectangular array - not ragged array" // (or may return length * witdh == 4*7 == 28) Also this would allow us to do int[5][6,7] array3; // ragged array of rectangular arrays (though this probably should not be allowed without further investigation : ? is it useful ? is it a pain to implement (probably) ? could it lead to errors ? do the implementation costs out weight the benifits )
 int my_array[56,23]; /*56*23 array */
 
 just like in functions? You dont see
 int my_func(6)("3ghfogho");
 do you?
 I figure that the co-ordinates are essentialy parameters.
 You see, you can also use an array as a function! (Though limited, but
 follow along)
 
 Let's say I want a function to compare a variable, foo to a constant qux.
 It returns 1 if it's higher and 0 if it's lower.
 
 Let's look at this in the ways I've described above:
 
 int qux = 10;
 
 int comparefoo(int foo)
 {
  if (foo<qux)
     return 1;
  else(foo=>qux)

why is the second condition needed on the else? should this be >= not => anyway? and why bother with the else at all in this example?
     return 0;
 }

D -> int qux = 10; // probably better as enum if constant int comparefoo(int foo) { if( foo<qux ) return 1; return 0; } Or are you trying to give an example of another problem?
 Now, as an array:
 
 int myarray[1] = {0,1};

D allows this. int[1] myarray = { 0, 1 }; // also allowed
 Yes, I know, the above wasn't proper D (I still think Mars is cooler) but
 still. You get my point?

As I do not know Mars (other than in it's incarnation as D) I could not comment. C 2002/7/20
Jul 20 2002
next sibling parent reply "anderson" <anderson firestar.com.au> writes:
"C.R.Chafer" <blackmarlin nospam.asean-mail.com> wrote in message
news:ahbjoc$28ot$1 digitaldaemon.com...
 Andrew Nguyen wrote:

 [][] is used to designate a ragged array.
 [,] could be implemented for rectangular arrays.

 so.
         int[4][7] array1;       // is 4 ptrs to arrays of int length 7

                         // (accesses required at least 1 pointer

         int[4,7] array2;        // is array of width 4, length 7 named

                         // (accesses do not require a pointer

                         //  but do required a multiply / shift and add -

                         //  these may be optimised away some times)

I agree, this makes good sense.
Jul 20 2002
parent "Sean L. Palmer" <seanpalmer earthlink.net> writes:
I like it.  I want this.

Sean

"anderson" <anderson firestar.com.au> wrote in message
news:ahbr09$2g8j$1 digitaldaemon.com...
 "C.R.Chafer" <blackmarlin nospam.asean-mail.com> wrote in message
 news:ahbjoc$28ot$1 digitaldaemon.com...
 Andrew Nguyen wrote:

 [][] is used to designate a ragged array.
 [,] could be implemented for rectangular arrays.

 so.
         int[4][7] array1;       // is 4 ptrs to arrays of int length 7

                         // (accesses required at least 1 pointer

         int[4,7] array2;        // is array of width 4, length 7 named

                         // (accesses do not require a pointer

                         //  but do required a multiply / shift and add -

                         //  these may be optimised away some times)

I agree, this makes good sense.

Jul 20 2002
prev sibling parent reply "Sandor Hojtsy" <hojtsy index.hu> writes:
 [][] is used to designate a ragged array.
 [,] could be implemented for rectangular arrays.

 so.
         int[4][7] array1;       // is 4 ptrs to arrays of int length 7

                         // (accesses required at least 1 pointer

         int[4,7] array2;        // is array of width 4, length 7 named

                         // (accesses do not require a pointer

                         //  but do required a multiply / shift and add -

                         //  these may be optimised away some times)

Man, this is way cool.
Jul 23 2002
parent reply andrew n <andrew_member pathlink.com> writes:
In article <ahj122$k31$1 digitaldaemon.com>, Sandor Hojtsy says...
 [][] is used to designate a ragged array.
 [,] could be implemented for rectangular arrays.

 so.
         int[4][7] array1;       // is 4 ptrs to arrays of int length 7

                         // (accesses required at least 1 pointer

         int[4,7] array2;        // is array of width 4, length 7 named

                         // (accesses do not require a pointer

                         //  but do required a multiply / shift and add -

                         //  these may be optimised away some times)


But then the only difference is that the [][] arrays are created by pointers and that the [,] are now just arrays that has data that is closer together! Now there is no point! It's like the difference between a struct and a class for god's sake!
Jul 23 2002
next sibling parent Pavel Minayev <evilone omen.ru> writes:
On Tue=2C 23 Jul 2002 18=3A27=3A12 +0000 =28UTC=29 andrew n
=3Candrew=5Fmember=40pathlink=2Ecom=3E 
wrote=3A

=3E In article =3Cahj122$k31$1=40digitaldaemon=2Ecom=3E=2C Sandor Hojtsy
says=2E=2E=2E
=3E But then the only difference is that the =5B=5D=5B=5D arrays are created by
pointers 
and
=3E that the =5B=2C=5D are now just arrays that has data that is closer
together! Now

There IS a difference=2E =5B=2C=5D array is =5Fguaranteed=5F to be rectangle
=28that is=2C each
row has the same size=29=2E There is no such guarantee for =5B=5D=5B=5D
arrays=2E So these are
two =5Ffundamentally=5F different data types=2E

Also=2C =5B=2C=5D arrays can be converted from static to dynamic and vice versa 
easily=2E 
This is not true for =5B=5D=5B=5D arrays=2E For example=2C try the following=3A

=09void calc=28double=5B=5D=5B=5D matrix=29=09
=09{
=09=09=2F=2F do something inside
=09}

=09double=5B3=5D=5B3=5D matrix=3B
=09calc=28matrix=29=3B

It won't work=2C for obvious reasons=2E And =5B=2C=5D arrays don't have such
problem=2E
Jul 23 2002
prev sibling parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"andrew n" <andrew_member pathlink.com> wrote in message
news:ahk760$iaa$1 digitaldaemon.com...

 But then the only difference is that the [][] arrays are created by

 that the [,] are now just arrays that has data that is closer together!

 there is no point! It's like the difference between a struct and a class

 god's sake!

Sometimes you need the data to be contiguous in memory, such as with screen buffers. The ragged arrays cannot be used then. The normal solution is to use a 1d array or a pointer, and calculate the index or offset yourself: int[1024*768] screen; void drawPixel (in int x, in int y, in int color) { int index = y * 1024 + x; screen[index] = color; } It would be cool if D could provide syntax sugar for this kind of thing, because it is quitte common. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Jul 23 2002
parent reply "Walter" <walter digitalmars.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:ahkor9$12dn$1 digitaldaemon.com...
 Sometimes you need the data to be contiguous in memory,
 such as with screen buffers. The ragged arrays cannot be
 used then. The normal solution is to use a 1d array or
 a pointer, and calculate the index or offset yourself:

 int[1024*768] screen;

 void drawPixel (in int x, in int y, in int color)
 {
    int index = y * 1024 + x;
    screen[index] = color;
 }

 It would be cool if D could provide syntax sugar for
 this kind of thing, because it is quitte common.

It already does: int[1024][768] screen;
Jul 24 2002
parent Pavel Minayev <evilone omen.ru> writes:
On Wed, 24 Jul 2002 22:41:53 -0700 "Walter" <walter digitalmars.com> wrote:

 It would be cool if D could provide syntax sugar for
 this kind of thing, because it is quitte common.

It already does: int[1024][768] screen;

It is so if you know the exact resolution when you write the program. But what if you want the user to be able to change it at run-time? Then, you'll need dynamic allocation anyhow...
Jul 24 2002
prev sibling parent reply Pavel Minayev <evilone omen.ru> writes:
On Fri=2C 19 Jul 2002 22=3A14=3A57 -0700 =22Andrew Nguyen=22
=3Cpythonnet=40hotmail=2Ecom=3E 
wrote=3A

=3E Also=2C =5B=5D=5B=5D=3F
=3E Why not seperate the dimensions with commas=3F IE=2C
=3E 
=3E int my=5Farray=5B56=2C23=5D=3B =2F*56*23 array *=2F

It makes more sense when applied to dynamic arrays=2E So=2C we could
have two distinct types=2C one for arrays of arrays - =5B=5D=5B=5D - and
another for rectangular arrays - =5B=2C=5D=2E Just like C#=2E

=09short=5B=2C=5D screenbuf=3B

=09if =28vidmode =3D=3D VGA50=29
=09=09screen =3D new short=5B80=2C 50=5D=3B
=09else
=09=09screen =3D new short=5B80=2C 25=5D=3B

I don't know a way to do it in D=2C currently=2E You can use =5B=5D=5B=5D=2C
but then the array won't be stored as a single block in memory=2C
and thus cannot be copied easily=2E On other hand=2C rectangular
arrays could be copied=3A

=09short=5B80=2C25=5D* screen =3D cast=28short=5B80=2C25=5D*=29 0xA0000=3B
=09=28*screen=29=5B=5D =3D screenbuf=5B=5D=3B
Jul 20 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374577734276852 news.digitalmars.com...
It makes more sense when applied to dynamic arrays. So, we could
have two distinct types, one for arrays of arrays - [][] - and
another for rectangular arrays - [,]. Just like C#.


D already supports rectangular arrays - just supply a dimension in the
brackets. No need for a separate syntax.
Jul 21 2002
next sibling parent pythonnet hotmail.com writes:
In article <ahfot9$4d2$1 digitaldaemon.com>, Walter says...
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374577734276852 news.digitalmars.com...
It makes more sense when applied to dynamic arrays. So, we could
have two distinct types, one for arrays of arrays - [][] - and
another for rectangular arrays - [,]. Just like C#.


D already supports rectangular arrays - just supply a dimension in the
brackets. No need for a separate syntax.

Jul 21 2002
prev sibling next sibling parent reply pythonnet hotmail.com writes:
In article <ahfot9$4d2$1 digitaldaemon.com>, Walter says...
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374577734276852 news.digitalmars.com...
It makes more sense when applied to dynamic arrays. So, we could
have two distinct types, one for arrays of arrays - [][] - and
another for rectangular arrays - [,]. Just like C#.


D already supports rectangular arrays - just supply a dimension in the
brackets. No need for a separate syntax.

Sorry for the repost, my finger slipped :) But... couldn't ragged arrays SAVE memory? Like in a string array or something of that sort.
Jul 21 2002
parent Pythonnet <Pythonnet_member pathlink.com> writes:
(For userfriendly readers.) I am thinkink dat da java bashink was not good idea.
Da?

*tugs at shirt collar* I think Im regretting sending that email to Borland...
I kinda bashed Java... IMHO it's a piece of shik. But uh... I don't think it'll
stifle my plan of bringing D to the masses. Da?
Jul 21 2002
prev sibling parent reply Pavel Minayev <evilone omen.ru> writes:
On Sun=2C 21 Jul 2002 18=3A51=3A06 -0700 =22Walter=22
=3Cwalter=40digitalmars=2Ecom=3E wrote=3A

=3E D already supports rectangular arrays - just supply a dimension in the
=3E brackets=2E No need for a separate syntax=2E

That'd be only partially dynamic=3A

=09int=5B=5D=5B5=5D array=3B

If I get it right=2C it is stored in the memory in the following way=3A

=09  4 bytes=09- length =28L=29
=09L*5 bytes=09- data

What I suppose is=2C when you use the =5B=2C=5D syntax=2C the array uses
=5Ftwo=5F
ints to store =5Ftwo=5F dimensions=3A

=09int=5B=2C=5D array=3B

The memory layout would be=3A

=09    4 bytes=09- width =28W=29
=09    4 bytes=09- height =28H=29
=09W*H*5 bytes=09- data

So=2C =5Fboth=5F width and height can change at run-time=2E This solves another
problem - remember the complaints about the fact that static multidimensional
arrays cannot be converted to dynamic ones=3F

=09int det=28double=5B=5D=5B=5D matrix=29 { =2E=2E=2E }
=09
=09double matrix=5B3=5D=5B3=5D=3B
=09det=28matrix=29=3B=09=2F=2F error!

Well=2C with the syntax I propose=2C you can=3A
=09
=09int det=28double=5B=2C=5D matrix=29 { =2E=2E=2E }

=09double matrix=5B3=2C3=5D=3B
=09det=28matrix=29=3B=09=2F=2F okay!

Converting such static array to dynamic is as simple as converting
multidimensional arrays=3A just prepend width and height=2E
Jul 21 2002
parent reply "Walter" <walter digitalmars.com> writes:
Ok, I see what you mean.

"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374594251942477 news.digitalmars.com...
On Sun, 21 Jul 2002 18:51:06 -0700 "Walter" <walter digitalmars.com> wrote:

 D already supports rectangular arrays - just supply a dimension in the
 brackets. No need for a separate syntax.

That'd be only partially dynamic: int[][5] array; If I get it right, it is stored in the memory in the following way: 4 bytes - length (L) L*5 bytes - data What I suppose is, when you use the [,] syntax, the array uses _two_ ints to store _two_ dimensions: int[,] array; The memory layout would be: 4 bytes - width (W) 4 bytes - height (H) W*H*5 bytes - data So, _both_ width and height can change at run-time. This solves another problem - remember the complaints about the fact that static multidimensional arrays cannot be converted to dynamic ones? int det(double[][] matrix) { ... } double matrix[3][3]; det(matrix); // error! Well, with the syntax I propose, you can: int det(double[,] matrix) { ... } double matrix[3,3]; det(matrix); // okay! Converting such static array to dynamic is as simple as converting multidimensional arrays: just prepend width and height.
Jul 22 2002
next sibling parent Pavel Minayev <evilone omen.ru> writes:
On Mon, 22 Jul 2002 11:50:31 -0700 "Walter" <walter digitalmars.com> wrote:

 Ok, I see what you mean.

By the way, I've seen it in C#.
Jul 22 2002
prev sibling parent "anderson" <anderson firestar.com.au> writes:
Of course you should go further then just 2D (not to be confused with matrix
2d).

int[w,h,l] array;
ect...

"Walter" <walter digitalmars.com> wrote in message
news:ahhm2a$244l$1 digitaldaemon.com...
 Ok, I see what you mean.

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:CFN374594251942477 news.digitalmars.com...
 On Sun, 21 Jul 2002 18:51:06 -0700 "Walter" <walter digitalmars.com>

 D already supports rectangular arrays - just supply a dimension in the
 brackets. No need for a separate syntax.

That'd be only partially dynamic: int[][5] array; If I get it right, it is stored in the memory in the following way: 4 bytes - length (L) L*5 bytes - data What I suppose is, when you use the [,] syntax, the array uses _two_ ints to store _two_ dimensions: int[,] array; The memory layout would be: 4 bytes - width (W) 4 bytes - height (H) W*H*5 bytes - data So, _both_ width and height can change at run-time. This solves another problem - remember the complaints about the fact that static multidimensional arrays cannot be converted to dynamic ones? int det(double[][] matrix) { ... } double matrix[3][3]; det(matrix); // error! Well, with the syntax I propose, you can: int det(double[,] matrix) { ... } double matrix[3,3]; det(matrix); // okay! Converting such static array to dynamic is as simple as converting multidimensional arrays: just prepend width and height.

Jul 22 2002