June 15, 2005 LOL - int array literals (and an idea) | ||||
|---|---|---|---|---|
| ||||
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 ) )
:)
| ||||
June 15, 2005 Re: LOL - int array literals (and an idea) | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply