Thread overview
[Issue 8008] New: static array literal syntax request: auto x=[1,2,3]S;
Apr 30, 2012
Tove
May 31, 2013
Shriramana Sharma
May 31, 2013
Andrej Mitrovic
April 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8008

           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
        AssignedTo: nobody@puremagic.com
        ReportedBy: thelastmammoth@gmail.com


--- Comment #0 from thelastmammoth@gmail.com 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
int)
 * 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];
   fun(x);


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]

Advantages:
 * 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
write
   fun([1,2,3]S);
or
   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: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8008


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2012-04-30 14:19:21 PDT ---
Aesthetically-wise this new literal syntax is not so good, but it solves a problem, I think it doesn't introduce problems, and it's effective, so it's an interesting idea:

auto a1 = [1, 2, 3]s;
auto a2 = [1, 2, 3]S;
auto s1 = "abc"s;
auto a3 = ["abc"ds, "def"ds]s; // dchar[3][2] ?

A problem is that "static array" is not a correct term in D, because here foo is a static array but it's not what you are writing about:

void main() {
    static foo = [1, 2];
}

So I generally call them fixed-sized arrays. So an alternative is to use a different letter to denote them (I don't think they get confused with floats):

auto a1 = [1, 2, 3]f;
auto a2 = [1, 2, 3]f;
auto s1 = "abc"f;
auto a3 = ["abc"f, "def"f]f;


The [$] was shown in Issue 481  and elsewhere.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 30, 2012
On Monday, 30 April 2012 at 21:18:11 UTC, bearophile_hugs@eml.cc wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=8008
> The [$] was shown in Issue 481  and elsewhere.

I do find the [$] syntax more elegant/intuitive...
  auto[$] a = [1, 2, 3];
... but couldn't we also solve this in library code?

auto a = [1, 2, 3].toStatic;

... as long as the compiler would optimize away the unused dynamic literal...

May 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8008



--- Comment #2 from thelastmammoth@gmail.com 2012-05-30 13:03:09 PDT ---
Thanks for the reply, I wish this could receive more attention...
If you're worried about esthetics, how about using a distinct symbol instead of
a letter to make it pop out:
For example:

auto x=[1,2,3]@; fun([1,2,3]@);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8008


Shriramana Sharma <samjnaa@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samjnaa@gmail.com


--- Comment #3 from Shriramana Sharma <samjnaa@gmail.com> 2013-05-31 10:18:19 PDT ---
(In reply to comment #2)
> Thanks for the reply, I wish this could receive more attention...
> If you're worried about esthetics, how about using a distinct symbol instead of
> a letter to make it pop out:
> For example:
> auto x=[1,2,3]@;
> fun([1,2,3]@);

I also don't like [,,,]S syntax. But i.o. @ I think we can re-use the $ which is suggested for specifying in the type as in bug 481.

But even then, such a special syntax would only be needed if it is necessary for specifying in a literal that a fixed-size array is intended. In assignment or other expressions it would not be required but only in direct function arguments.

And even then, why wouldn't a function that expects a fixedsize array not accept a dynamic one of appropriate size? If it doesn't accept, then probably that is what should be fixed and no new syntax for such fixed-size array literals is needed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8008


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-05-31 10:27:09 PDT ---
(In reply to comment #0)
>  * no easy way to pass a static array literal to a function accepting a static
> array; for example it requires:
>    int[3] x=[1,2,3];
>    fun(x);

That can't be true. This works fine:

-----
void fun(int[3] arr) { }

void main()
{
    fun([1, 2, 3]);
}
-----

I've tried it with 2.050, and it worked there.

Additionally since 2.063 you can also do:

-----
void fun(int[3] arr) { }

void main()
{
    auto x = [1, 2, 3];
    fun(x[0 .. 3]);
}
-----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------