On Thursday, 4 July 2019 at 15:10:05 UTC, aliak wrote:
Any ideas on how to be able to do something like this?

struct S(alias _fun) {
  alias Fun = _fun;
}

void algorithm(alias f, T)(T s) {
  static if (&f == &T.Fun) {
    // trivial return
  } else {
    // must perform work, then return
  }
}

Can you use function addresses in some way? I've seen that some of them are resolved to __lambda0 and other times to a function type, i.e.

Error: incompatible types for (& f) is (& __lambda1): void function(A!(function () => 3) t) and int function() pure nothrow @nogc @safe

That comes from doing doing this:

alias g = () => 3;
algorithm!g(S!g());

I've thought of something along the lines of a function alias wrapper. But I'm not sure how to make that work either. Something like:

struct Fun(alias _fun, string _id) {
  alias Fun = _fun;
  enum ID = _id;
}

Then maybe something like:

alias g = () => 3;
Fun!(g, "myid") gfun;
algorithm!gfun(S!gfun());

Then inside algorithm the check becomes:

static if (&f.ID == &T.Fun.ID)

But then I'd like to generate the ID at instantiation point. So is using __LINE__, __FILE__ and applying some hash function a good idea here?

Any other ideas?

Cheers,
- Ali

PS: If you're curious of a semi working sample, to see what I'm actually trying to do, I've put up this gist that does not compile right now:

https://gist.github.com/aliak00/fcdd4fa7512035405bb7015cf6d8016f

I don't know if it will solve your whole problem, but have you tried __traits(isSame, W0.fun, fun)?

Reduced example:

struct Foo(alias fun){
    alias bar = fun;
}

void stuff(alias fun, T)(T t)
{
    static if(__traits(isSame, fun, T.bar)) {
        pragma(msg, "Yes");
    } else {
        pragma(msg, "No");
    }
}

void a(){}
void b(){}

void main()
{
    stuff!a(Foo!a()); // Yes
    stuff!a(Foo!b()); // No
}

Reply via email to