www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - LOL - int array literals (and an idea)

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
Look at this healthy mess:

void main()
{
 int[] 
arr=(cast(int*)cast(void*)(cast(dchar[])"\u0001\u0002\u0003").ptr)[0..3];
 for(uint i=0; i<arr.length; i++)
  writefln(arr[i]);
}

It prints

1
2
3

It works!

Just something interesting I thought up in the limbo of half-sleep this 
morning.  But it does point out something interesting - how is it that 
(d|w)char arrays can have non-static literal initializers, but not anything 
else?  I propose an "array" keyword, that works something like the 
"function" or "delegate" keywords:

int[] arr=int array[1,2,5,6,9];

Typechecking could be done on the contents of the literal because of the 
"int", it could be used as an argument to a function, and the array keyword 
makes it unambiguous.  And then we can use the array keyword in "is" 
expressions!

if( is( arr == array ) )

 :) 
Jun 15 2005
parent Chris Sauls <ibisbasenji gmail.com> writes:
Jarrett Billingsley wrote:
 void main()
 {
  int[] 
 arr=(cast(int*)cast(void*)(cast(dchar[])"\u0001\u0002\u0003").ptr)[0..3];
  for(uint i=0; i<arr.length; i++)
   writefln(arr[i]);
 }

Cute... throw in hex strings and it gets even cuter. :) # arr = (cast(int*)cast(void*)(cast(dchar[])x"01 02 03").ptr)[0..3]; -- Chris Sauls
Jun 15 2005