Initializing static arrays without specifying size
bearophile
bearophileHUGS at lycos.com
Mon Aug 2 05:15:39 PDT 2010
Philippe Sigaud:
> Would a template-based solution be OK?
>
> import std.stdio, std.traits;
>
> CommonType!T[T.length] staticArray(T...)(T vals)
> if ((T.length > 0) && is(CommonType!T))
> {
> return [vals];
> }
That's one solution, but code like that is most useful when your arrays liters are long (because if they are little you can just count the items and avoid using staticArray), this is an example (I have used printf to avoid the metric ton of functions and templates used by writeln):
import std.c.stdio: printf;
import std.traits: CommonType;
CommonType!T[T.length] staticArray(T...)(T vals)
if (T.length && is(CommonType!T)) {
return [vals];
}
void main() {
auto a = staticArray(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230,
240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340,
350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450,
460, 470, 480, 490, 500, 510, 520, 530, 540, 550, 560,
570, 580, 590, 600, 610, 620, 630, 640, 650, 660, 670,
680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780,
790, 800, 810, 820, 830, 840, 850, 860, 870, 880, 890,
900, 910, 920, 930, 940, 950, 960, 970, 980, 990);
printf("%d\n", a[2]);
}
This is the asm of the staticArray:
_D5test2217__T11staticArrayTiÉÛÿ ÀÀÈÈZÖFiiÉÛÿ ¤¤ZG100i comdat
L0: push EAX
push ESI
push EDI
push dword ptr 010h[ESP]
push dword ptr 018h[ESP]
push dword ptr 020h[ESP]
push dword ptr 028h[ESP]
push dword ptr 030h[ESP]
push dword ptr 038h[ESP]
push dword ptr 040h[ESP]
push dword ptr 048h[ESP]
push dword ptr 050h[ESP]
push dword ptr 058h[ESP]
push dword ptr 060h[ESP]
push dword ptr 068h[ESP]
push dword ptr 070h[ESP]
push dword ptr 078h[ESP]
push dword ptr 080h[ESP]
push dword ptr 088h[ESP]
push dword ptr 090h[ESP]
push dword ptr 098h[ESP]
push dword ptr 0A0h[ESP]
push dword ptr 0A8h[ESP]
push dword ptr 0B0h[ESP]
push dword ptr 0B8h[ESP]
push dword ptr 0C0h[ESP]
push dword ptr 0C8h[ESP]
push dword ptr 0D0h[ESP]
push dword ptr 0D8h[ESP]
push dword ptr 0E0h[ESP]
push dword ptr 0E8h[ESP]
push dword ptr 0F0h[ESP]
push dword ptr 0F8h[ESP]
push dword ptr 0100h[ESP]
push dword ptr 0108h[ESP]
push dword ptr 0110h[ESP]
push dword ptr 0118h[ESP]
push dword ptr 0120h[ESP]
push dword ptr 0128h[ESP]
push dword ptr 0130h[ESP]
push dword ptr 0138h[ESP]
push dword ptr 0140h[ESP]
push dword ptr 0148h[ESP]
push dword ptr 0150h[ESP]
push dword ptr 0158h[ESP]
push dword ptr 0160h[ESP]
push dword ptr 0168h[ESP]
push dword ptr 0170h[ESP]
push dword ptr 0178h[ESP]
push dword ptr 0180h[ESP]
push dword ptr 0188h[ESP]
push dword ptr 0190h[ESP]
push dword ptr 0198h[ESP]
push dword ptr 01A0h[ESP]
push dword ptr 01A8h[ESP]
push dword ptr 01B0h[ESP]
push dword ptr 01B8h[ESP]
push dword ptr 01C0h[ESP]
push dword ptr 01C8h[ESP]
push dword ptr 01D0h[ESP]
push dword ptr 01D8h[ESP]
push dword ptr 01E0h[ESP]
push dword ptr 01E8h[ESP]
push dword ptr 01F0h[ESP]
push dword ptr 01F8h[ESP]
push dword ptr 0200h[ESP]
push dword ptr 0208h[ESP]
push dword ptr 0210h[ESP]
push dword ptr 0218h[ESP]
push dword ptr 0220h[ESP]
push dword ptr 0228h[ESP]
push dword ptr 0230h[ESP]
push dword ptr 0238h[ESP]
push dword ptr 0240h[ESP]
push dword ptr 0248h[ESP]
push dword ptr 0250h[ESP]
push dword ptr 0258h[ESP]
push dword ptr 0260h[ESP]
push dword ptr 0268h[ESP]
push dword ptr 0270h[ESP]
push dword ptr 0278h[ESP]
push dword ptr 0280h[ESP]
push dword ptr 0288h[ESP]
mov ECX,offset FLAT:_D14TypeInfo_G100i6__initZ
push dword ptr 0290h[ESP]
push dword ptr 0298h[ESP]
push dword ptr 02A0h[ESP]
push dword ptr 02A8h[ESP]
push dword ptr 02B0h[ESP]
push dword ptr 02B8h[ESP]
push dword ptr 02C0h[ESP]
push dword ptr 02C8h[ESP]
push dword ptr 02D0h[ESP]
push dword ptr 02D8h[ESP]
push dword ptr 02E0h[ESP]
push dword ptr 02E8h[ESP]
push dword ptr 02F0h[ESP]
push dword ptr 02F8h[ESP]
push dword ptr 0300h[ESP]
push dword ptr 0308h[ESP]
push dword ptr 0310h[ESP]
push dword ptr 0318h[ESP]
push dword ptr 0320h[ESP]
push dword ptr 0328h[ESP]
push 064h
push ECX
call near ptr __d_arrayliteralT
mov ESI,EAX
mov EDI,01A0h[ESP]
mov ECX,064h
rep
movsd
add ESP,0198h
mov EAX,8[ESP]
pop EDI
pop ESI
pop ECX
ret 0190h
And you essentially have one similar function each time you use staticArray. This is why in my answer I have said I don't know any good solution to this problem and this is why I have proposed the enhancement [$] syntax.
In my code I count the items in some way (with Python, or I assign the literal to a dynamic array, print its length and then modify the code replacing the dynamic array with the printed number).
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list