Summary: static array literal syntax request: auto x=[1,2,3]S;
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2012-04-30 14:04:10 PDT ---
Currently, array literals such as 
   auto x=[1,2,3];
make x dynamic. To get a static array one needs to write: 
   int[3] x=[1,2,3];
which is inconvenient for many reasons:
 * DRY principle (need to explicitly write 3 as the length and specify the type
 * no easy way to pass a static array litteral to a function accepting a static
array; for example it requires:
   int[3] x=[1,2,3]; 

Wouldn't it be simple to allow writing array literals using the syntax:
   auto x=[1,2,3]S; 
where S stands for static?
More generally the compiler should translate [x1,...,xn]S to: typeof(x1)[n]

 * static array litterals becomes as convenient as dynamic ones
 * no confusion possible for the compiler; I believe this syntax doesn't clash
with existing syntax.
 * In our previous example, no need to write an intermediate x: we can just
   fun([1.0,2,3]S); //for example, if static array of doubles requested 

* this would also prevent the common workaround hacks of the form:
   void fun(T...)(T x){} which accept fun(1,2,3): one could just write:
   void fun(T,uint N)(in T[N]x){} or void fun(T,uint N)(T[N]x){}

* this could prevent inefficient intermediate code as reported in Issue 2356
and related, as it would be clear from "S" that a static is requested. 

* this could be used in expressions as well: auto x=[1,2,3]S+[4,5,6]S;

This should be simpler than a previous request I've seen for int[$]x=[1,2,3];,
which still requires one to write the type explicitly.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to