A nice way to step into 2012

bearophile bearophileHUGS at lycos.com
Tue Dec 27 01:35:44 PST 2011


Andrei Alexandrescu:

> https://github.com/D-Programming-Language/dmd/commit/675898721c04d0bf155a85abf986eae99c37c0dc

Since some time thanks to a post in this newsgroups I am able to compile DMD, so I am able to test the changes early. It seems to work in various situations:


import std.stdio, std.range, std.algorithm;
int foo(alias callMe)(int a) {
    return callMe(a);
}
int bar(alias callMe)() {
    return callMe(1, 10, 100);
}
void main() {
    auto r = foo!((x) => x ^^ 2)(4);
    assert(r == 16);
    r = foo!((x,) => x ^^ 2)(5);
    assert(r == 25);
    // r = foo!((x,,) => x ^^ 2)(5); // error, good
    r = foo!(x=>x^^2)(6);
    assert(r == 36);
    r = bar!((x, y, z) => x + y + z)();
    assert(r == 111);
    double delegate(double)[] f;
    f ~= (double x) => x ^^ 2;
    assert(f[0](10) == 100);
    // Syntax variety, fit for everyone:
    auto pow4 = (int x) => x ^^ 4;
    writeln(map!(function int(in int x) pure nothrow { return x ^^ 4; })(iota(10)));
    writeln(map!(function int(int x){ return x ^^ 4; })(iota(10)));
    writeln(map!(function(int x){ return x ^^ 4; })(iota(10)));
    writeln(map!((int x){ return x ^^ 4; })(iota(10)));
    writeln(map!((x){ return x ^^ 4; })(iota(10)));
    writeln(map!((int x) => x ^^ 4)(iota(10)));
    writeln(map!((x) => x ^^ 4)(iota(10)));
    writeln(map!(x => x ^^ 4)(iota(10)));
    writeln(map!q{ a ^^ 4 }(iota(10)));
    writeln(map!"a^^4"(iota(10)));
    writeln(map!pow4(iota(10)));
}

-----------------------

This program contains wrong syntax because x lacks a type:

void main() {
    double delegate(double) f;
    f = (x) => x + 1;
}


DMD gives the error messages:

test.d(3): Error: undefined identifier x, did you mean variable f?
test.d(3): Error: cannot implicitly convert expression (__dgliteral1) of type _error_ delegate(_error_) to double delegate(double)

Is it wise to try to improve the second error message?

Bye,
bearophile


More information about the Digitalmars-d mailing list