On Friday, 20 November 2020 at 14:08:23 UTC, jmh530 wrote:
Doing something like below fails because I don't seem to be able to make a templated lambda that just takes types. Is the only way to do something similar to create a separate function to handle the condition, or is there some other way to do something with similar flexibility?

import std.stdio: writeln;
import std.meta: allSatisfy;

void foo(Args...)(Args args)
    if (allSatisfy!(x => is(x == double), Args))
{
    writeln("works");
}

void main() {
    foo(1.0, 2.0);
}

There is no way to create an anonymous template in D. You will have to declare a separate helper template:

    private enum isDouble(T) = is(T == double);

    void foo(Args...)(Args args)
        if (allSatisfy!(isDouble, Args))
    {
        // ...
    }

In this specific case, you could also make `foo` a type-safe variadic function [1], which would eliminate the need for `allSatisfy`:

    void foo(double[] args...)
    {
        // ...
    }

[1] https://dlang.org/spec/function.html#typesafe_variadic_functions

Reply via email to