Anyway to compare function aliases? Or any ideas?

aliak something at something.com
Thu Jul 4 15:10:05 UTC 2019


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


More information about the Digitalmars-d-learn mailing list