How to automatically generate function overloads

Blatnik blatblatnik at gmail.com
Tue May 4 11:12:22 UTC 2021


On Tuesday, 4 May 2021 at 11:00:42 UTC, Blatnik wrote:
> How could I do this?

I've already tried this:

```D
mixin template Vectorize_Unary_Function(alias Function)
{
   float[N] Function(size_t N)(float[N] vec)
   {
     float[N] result;
     static foreach (i; 0 .. N)
       result[i] = Function(vec[i]);
     return result;
   }
}
```

But it didn't work.

```D
mixin Vectorize_Unary_Function!clamp01;

float[2] vec = [1, 2];
float[2] clamped = clamp01(vec); // Error
```

I think it actually declared a function called "Function" instead 
of calling it "clamp01" like the alias I passed in.


More information about the Digitalmars-d-learn mailing list