Array initializers and inheritance

Stewart Gordon smjg_1998 at yahoo.com
Sat Nov 4 06:23:21 PST 2006


Frank Benoit (keinfarbton) wrote:
> I think, the array initialization feature is not so nice. The array
> takes its type from the type of the first initializer. This is
> absolutely ugly.

That's not initialization, that's array literals.

> I would prefer if you can write this
> _gameObjects = new GameObject [ new Player(), new Target(Direction.Up,
> 0, 0)];

That would create a parsing ambiguity.

But I agree that it would be nice if you could give the type of an array 
literal explicitly.  This syntax was suggested at least once:

     _gameObjects = new GameObject[]!
       [ new Player(), new Target(Direction.Up, 0, 0)];

> The second bad thing is, they tend to generate static arrays. So often
> you need to append a [] to convert the static array into a dynamic one.
> 
> When generating D code, i use array initialization and cast every member
> to the type of the array element and append [] to every such
> initializer. This code looks really ugly.
> 
> @W Please rethink this syntax :)

My thought is that the programmer should have the choice of 
implicitly-typed or explicitly-typed array literals.  And that 
implicitly-typed array literals should be simultaneously of every type 
to which every element is implicitly convertible, in much the same way 
as string literals are simultaneously of types char[], wchar[] and 
dchar[].  And be simultaneously of static and dynamic array types.

So for example,

     [ 42, 0, 6789 ]

would be of the following types:

short[3]
short[]
ushort[3]
ushort[]
int[3]
int[]
uint[3]
uint[]
long[3]
long[]
ulong[3]
ulong[]

and that's before you count the floating-point and complex versions....

> Or did I really missed the "good way of array initialization"?

Pardon?

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-learn mailing list