On Thursday, 16 February 2017 at 07:48:37 UTC, Jacob Carlborg wrote:
On 2017-02-16 01:40, John Colvin wrote:

Slightly OT, but it is related so bear with me:

Design by introspection is great and prevent having to make new names for every possible combo of features. Unfortunately, it doesn't work so well for introspecting multiple types at once: normally you have to then make a new symbol, with a new name, to pass to e.g. allSatisfy. That annoys me, so I've been wanting this sort of thing for a while (which would also make `every` way better to use, instead of having to make a
name for every predicate:

You know how we have named parameterised enums and aliases?
enum isInt(a) = is(a == int);
and
alias sizeof(a) = a.sizeof;

why not allow *anonymous* parameterised enums and aliases, like this:

enum areAllInt(TL ...) = allSatisfy!(enum(a) => is(a == int), TL);

void foo(TL...)(TL args)
if (allSatisfy!(enum(T) => T.sizeof <= 4, TL))
{
    // ...
}

I've been thinking something similar. But this is the way I see it:

A lambda is an anonymous template function which is not yet instantiated. But with the lambda syntax only one the different template parameters are allowed. Why not generalize the lambda syntax to allow different kind of template parameters. Example:

What we have today:

[1, 2, 3].map!(e => e * 2);

Is basically the same as:

T __anonymous(T)(T e)
{
    return e * 2;
}

[1, 2, 3].map!(__anonymous!(int));

But today we cannot represent the following template with a lambda:

size_t sizeof(alias a)()
{
    return a.sizeof;
}

So why not allow this lambda syntax:

alias sizeof = (alias a) => a.sizeof;

It would be nice if we could reuse the existing syntax for regular template parameters as well:

alias isInt = t => is(t == int);
auto b = isInt!(int);

If the above is not possible, we could add an additional parameter list to lambdas:

alias isInt = (t)() => is(t == int);

You can sort of do this even today. All you need is struct with template-ed opCall.

See https://github.com/ZombineDev/rxd/blob/v0.0.3/source/rxd/meta2/lambda.d#L12 and
https://github.com/ZombineDev/rxd/blob/v0.0.3/source/rxd/xf/xform.d#L54

This one of the few (only?) places that C++14 is better than D, because in C++14 polymorphic lamdas are first-class values, whereas in D alias function literals are templates (i.e. you can refer to them only by alias, can't assign them to enum or auto constants / variables).

Reply via email to