Proposal to add 'Elements of Programming' Concepts to std.traits

Philippe Sigaud philippe.sigaud at gmail.com
Sun Jun 17 06:53:32 PDT 2012


On Sun, Jun 17, 2012 at 3:11 PM, Guillaume Chatelet
<chatelet.guillaume at gmail.com> wrote:
> On 06/17/12 14:23, Philippe Sigaud wrote:

>> void main(){
>>    auto a = constrained!(x => x > 0)(1);
>>    auto b = a;
>>    auto c = b+1;
>>    auto d = c+a;
>>    a = b-d; // Boom!
>> }
>
> Very nice :)

The new syntax for lambdas is delightful to use. A bunch of aliases
should give you the X10 examples.

x => ... still has some problem as it's a template, but using a
standard module-level function gives functionalities akin to X10:

bool pos(int x) { return x > 0;}
alias Constrained!(int, pos) Positive; // works

// alias Constrained!(int, (int x) { return x > 0;}) Positive; // works
// alias Constrained!(int, (int x) => x > 0) Positive; // works
// but:
// alias Constrained!(int, (x) { return x > 0;}) Positive; // ka-boom!
// alias Constrained!(int, x => x > 0) Positive; // no such luck



enum OK = Positive(1); // OK
enum NOK = Positive(-1); // Compiling error, cool!

So this works. That's nice to know, I never used an assert inside a CT
evaluation.

Hmmm, validating XML and/or SQL queries at CT...


More information about the Digitalmars-d mailing list