Bizarre way to 'new' arrays
Jarrett Billingsley
kb3ctd2 at yahoo.com
Thu Jun 15 17:21:50 PDT 2006
I was looking through parse.c again, and in the ::parseNewExp() function, I
noticed something odd. Interested, I typed this in:
int[] x = new int[](4);
And it compiles and runs. Writing
writefln(x.length);
displays 4.
This is legal because a NewExpression can be defined as
'new' [(ArgumentList)] Type (ArgumentList)
So in the case of 'new int[](4)', int[] is parsed as the Type and the (4) is
parsed as the argument list. In fact, writing 'new int[4]' is just
syntactic sugar for 'new int[](4)'. This makes sense, as when you new an
array (or anything for that matter), you're really calling a function.
Sorry if this is a bit OT, but I thought it was interesting.
More information about the Digitalmars-d
mailing list