Should we remove int[$] before 2.067?

Kenji Hara via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 30 08:44:31 PST 2015


2015-01-30 23:47 GMT+09:00 Andrei Alexandrescu via Digitalmars-d <
digitalmars-d at puremagic.com>:

> Please advise.


The new feature, I call it "Partial type deduction", is not only for static
array length.
Examples:

void main()
{
    const[] a1 = [1,2,3];   // a mutable array of const ints
    static assert(is(typeof(a1) == const(int)[]));
    a1 ~= 4;                // OK
    static assert(!__traits(compiles, { a1[0] = 10; }));     // cannot
modify const

    immutable[][$] a2 = [[1,2], [3,4]];  // a static array of mutable
dynamic array of immutable ints
    static assert(is(typeof(a2) == immutable(int)[][2]));
    static assert(!__traits(compiles, { a2 ~= [[5,6]]; }));  // cannot
append to static array
    a2[0] = [7,8];          // OK
    assert(a2 == [[7,8], [3,4]]);
    static assert(!__traits(compiles, { a2[0][0] = 100; })); // cannot
modify immutable
}

The type deduction will provide powerful way to type variables.
Yes, ultimately they can be replaced with library function calls, but the
call will be ugly and hard to understand. Consider making a2 by using
library function. Can you show us a concept design for that?

And, staticArray function will not work for the following case:

int function(int)[$] funcs = [
    a => a + 1,
    a => a * 2,
];

The template lambdas have no type until they applied to the type `int
function(int)`. So

auto funcs = staticArray(
    a => a + 1,
    a => a * 2,
);

is clearly impossible.

The core of the feature is a pattern matching for the declared variable
type. I believe it will be useful for declarative style programming.

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20150131/6f6ff44f/attachment.html>


More information about the Digitalmars-d mailing list