On Wednesday, 15 February 2017 at 19:39:52 UTC, Andrei Alexandrescu wrote:
On 02/15/2017 06:20 AM, Daniel N wrote:
On Wednesday, 15 February 2017 at 09:22:14 UTC, Daniel N wrote:
template every(T...)
{
    template satisfies(U...)
    {
        enum satisfies = true;
    }
}

(lunch-break => time to hack D!)

template every(T...)
{
  template satisfies(U...)
  {
    enum satisfies = {
      foreach(t; T)
        foreach(u; U)
      if(!u!t)
            return false;
      return true;
    }();
  }
}

That looks pretty neat. Can you find 4-5 cases in which this could be used gainfully in Phobos? Thanks! -- Andrei

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))
{
    // ...
}

an example from phobos:

bool ordered(alias less = "a < b", T...)(T values)
if (T.length == 2 && is(typeof(binaryFun!less(values[1], values[0])) : bool) || T.length > 2 && is(typeof(ordered!less(values[0..1 + $ / 2]))) && is(typeof(ordered!less(values[$ / 2..$]))))

becomes this:

bool ordered(alias less = "a < b", T...)(T values)
if (T.length > 2 &&
    allSatisfy!(enum(size_t i) =>
is(typeof(binaryFun!less(values[i], values[i+1])) : bool),
        Iota!(T.length - 1))

I admit it's not a huge win, but hiding the log_2(N) template depth behind allSatisfy is great: you shouldn't have to (and people wont) think about that when writing a template constraint like this.

There is a part of me that thinks all this meta-template stuff is madness though and that modifications to ctfe could make it mostly obsolete, but that's a story for later...

Reply via email to