Jump to page: 1 2
Thread overview
[Issue 8008] Syntax for fixed size array literals like [1,2,3]s
[Issue 8008] static array literal syntax request: auto x=[1,2,3]S;
Dec 16, 2014
Nick Treleaven
Dec 27, 2014
Nick Treleaven
Dec 27, 2014
rswhite4@gmail.com
Dec 27, 2014
rswhite4@gmail.com
Dec 28, 2014
Nick Treleaven
Dec 28, 2014
Andrej Mitrovic
Jun 06, 2015
naptime
Apr 27, 2016
Nick Treleaven
Dec 17, 2022
Iain Buclaw
December 16, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

Nick Treleaven <ntrel-pub@mybtinternet.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ntrel-pub@mybtinternet.com

--- Comment #5 from Nick Treleaven <ntrel-pub@mybtinternet.com> ---
Is there an advantage of literal syntax vs. a template function staticArray(1,
2)?

--
December 16, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #6 from bearophile_hugs@eml.cc ---
(In reply to Nick Treleaven from comment #5)
> Is there an advantage of literal syntax vs. a template function
> staticArray(1, 2)?

There are various small advantages. A template function like that seems to generate template bloat.

It's also longer to write:

[[1,2]s, [1,2]s, [1,2]s, [1,2]s]

versus:

[staticArray(1,2), staticArray(1,2), staticArray(1,2), staticArray(1,2)]

And probably []s is also faster to compile by the compiler.

Also, staticArray returns a fixed-size array, that according to the D ABI is a pointer, and the compiler has to inline the call to staticArray to regain efficiency. By default the D compiler inlines only if you use -inline. The []s lacks this problem and is efficient at all compilation levels.

The []s will probably become safe to use once D has some kind of tracking of memory ownership.

There are also some disadvantages, like a little more complexity in the D language.

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #7 from bearophile_hugs@eml.cc ---
If you want to generate certain i,i+10 pairs you can use this, but it generates lot of not efficient allocations:

void main() @nogc {
    import std.range: iota;
    import std.algorithm: map;
    auto pairs = 10.iota.map!(i => [i, i + 10]);
    foreach (p; pairs) {} // Isn't @nogc.
}


You can solve it using a little wrapper struct, but the result isn't an array despite the alias this:

void main() @nogc {
    import std.range: iota;
    import std.algorithm: map;
    static struct Pair { int[2] a; alias a this; }
    auto pairs = 10.iota.map!(i => Pair([i, i + 10]));
    foreach (p; pairs) {} // OK
}


You can also use this, but casts are unsafe things and it's better to minimize their usage:

void main() @nogc {
    import std.range: iota;
    import std.algorithm: map;
    auto pairs = 10.iota.map!(i => cast(int[2])[i, i + 10]);
    foreach (p; pairs) {} // OK
}


the []s syntax is handy to solve the problem, avoiding to create and manage a new type and keeping all safety:

void main() @nogc {
    import std.range: iota;
    import std.algorithm: map;
    auto pairs = 10.iota.map!(i => [i, i + 10]s);
    foreach (p; pairs) {} // OK
}

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|static array literal syntax |Syntax for fixed size array
                   |request: auto x=[1,2,3]S;   |literals like [1,2,3]s

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #8 from Nick Treleaven <ntrel-pub@mybtinternet.com> ---
(In reply to bearophile_hugs from comment #6)
> (In reply to Nick Treleaven from comment #5)
> > Is there an advantage of literal syntax vs. a template function
> > staticArray(1, 2)?
> 
> There are various small advantages. A template function like that seems to generate template bloat.

Yes, but this is already pervasive in Phobos.

> It's also longer to write:
> 
> [[1,2]s, [1,2]s, [1,2]s, [1,2]s]
> 
> versus:
> 
> [staticArray(1,2), staticArray(1,2), staticArray(1,2), staticArray(1,2)]

alias s = staticArray;

> And probably []s is also faster to compile by the compiler.

Probably not noticeably.

> Also, staticArray returns a fixed-size array, that according to the D ABI is a pointer, and the compiler has to inline the call to staticArray to regain efficiency. By default the D compiler inlines only if you use -inline. The []s lacks this problem and is efficient at all compilation levels.

This is another general problem. Inlining small template functions by default might be nice, if it doesn't slow the compiler too much, and could help avoid template bloat. But passing -inline isn't too much to ask IMO.

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

rswhite4@gmail.com changed:

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

--- Comment #9 from rswhite4@gmail.com ---
> the []s syntax is handy to solve the problem, avoiding to create and manage a new type and keeping all safety:
> 
> void main() @nogc {
>     import std.range: iota;
>     import std.algorithm: map;
>     auto pairs = 10.iota.map!(i => [i, i + 10]s);
>     foreach (p; pairs) {} // OK
> }

What do you think about:
----
import std.stdio;

auto ref U[N] s(U, alias N)(auto ref U[N] arr) pure nothrow @safe @property
@nogc
    if (__traits(compiles, { size_t i = N; }))
{
    return arr;
}

void test1(T)(T[3] arr) {

}

void test2(T)(auto ref T[3] arr) {

}

void main() {
    auto a = s([1, 2, 3]);
    writeln(typeid(a));

    auto b = [1, 2, 3].s;
    writeln(typeid(b));

    test1([1, 2, 3]);
    test1([1, 2, 3].s);
    test2([1, 2, 3].s);
}
----

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #10 from bearophile_hugs@eml.cc ---
(In reply to rswhite4 from comment #9)

> auto ref U[N] s(U, alias N)(auto ref U[N] arr) pure nothrow @safe @property
> @nogc
> 	if (__traits(compiles, { size_t i = N; }))

Simpler:

auto ref U[N] s(U, size_t N)(auto ref U[N] arr)
pure nothrow @safe @property @nogc {
    return arr;
}

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #11 from rswhite4@gmail.com ---
(In reply to bearophile_hugs from comment #10)
> (In reply to rswhite4 from comment #9)
> 
> > auto ref U[N] s(U, alias N)(auto ref U[N] arr) pure nothrow @safe @property
> > @nogc
> > 	if (__traits(compiles, { size_t i = N; }))
> 
> Simpler:
> 
> auto ref U[N] s(U, size_t N)(auto ref U[N] arr)
> pure nothrow @safe @property @nogc {
>     return arr;
> }

Of Course! Sometimes I think way too complicated.

--
December 27, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #12 from bearophile_hugs@eml.cc ---
(In reply to Nick Treleaven from comment #8)

> Yes, but this is already pervasive in Phobos.

I suggest to not make the situation worse.


> Probably not noticeably.

I don't believe this much. They are all templates with different length and type, and array literals are everywhere.


> Inlining small template functions by default might be nice,

This ER isn't asking for a template function.

Don't try to replace features that should be built-in with tons of templates, you end like the std.typecons.Tuple fiasco.

--
December 28, 2014
https://issues.dlang.org/show_bug.cgi?id=8008

--- Comment #13 from Nick Treleaven <ntrel-pub@mybtinternet.com> ---
The point is the templates can be inlined, and the bloat *could* be removed by the optimizer.

--
« First   ‹ Prev
1 2