[Issue 8008] Syntax for fixed size array literals like [1,2,3]s

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Dec 27 10:16:37 PST 2014


https://issues.dlang.org/show_bug.cgi?id=8008

rswhite4 at gmail.com changed:

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

--- Comment #9 from rswhite4 at 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);
}
----

--


More information about the Digitalmars-d-bugs mailing list