Template mixins and selective imports

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 3 08:29:47 PDT 2017


I am trying to create a vectorize function that mixes in a new 
version of function with the same name that applies the function 
(to an ndslice).

The code below compiles without error and has the behavior I 
would expect.

However, when I change the function import to a selective import 
(e.g. from import std.math; to import std.math : sin;), then I 
get errors implying that the overload is not actually created.

Ideally, I would like to get this working with any kind of import.

One thing I noticed is that the fullyQualifiedName of sin changes 
quite a bit depending on whether you use one or the other.

This may be related to either of the following:
https://issues.dlang.org/show_bug.cgi?id=16287
https://issues.dlang.org/show_bug.cgi?id=16023



-----------------------------------

import std.traits : isFunction;

private enum _vectorize(string f) = "
     import mir.ndslice.slice : SliceKind, Slice;

     auto " ~ f ~ "(SliceKind kind, size_t[] packs, Iterator)
                                            (Slice!(kind, packs, 
Iterator) slice)
     {
         import mir.ndslice.topology : map;
         return slice.map!(f);
     }
";

mixin template vectorize(alias f)
     if (isFunction!f)
{
     mixin(_vectorize!(__traits(identifier, f)));
}

unittest
{
     import std.stdio : writeln;
     import mir.ndslice.slice : sliced;
     import mir.ndslice.topology : map;
     import std.math; //import std.math : sin;

     auto x = [0.0, 1.0, 2.0, 3.0].sliced;

     auto y = x.map!(sin);
     writeln(y);

     mixin vectorize!(sin);
     auto z = sin(x);
     writeln(z);
}


More information about the Digitalmars-d-learn mailing list