Get and add to function body

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 13 12:37:36 UTC 2017


On 10/12/17 7:33 PM, Psychological Cleanup wrote:
> Is there any way to get the function body of a function, delegate, and 
> lambda? I'd also like to extend functions by "wrapping" them at compile 
> time generically. For example, I'd like to get all the properties of a 
> class and add some code to them(sort of like adding a scope, prolog, 
> and/or epilog).
> 
> Is any of this possible in D?

Something like this may work:

struct S(T)
{
    private T obj;
    auto ref opDispatch(string item, Args...)(auto ref Args args) {
        prolog();
        scope(exit) epilog();
        mixin("return obj." ~ item ~ args.length == 0 ? ";" : "(args);");
    }
}

Probably needs some more machinery to work for everything.

-Steve


More information about the Digitalmars-d-learn mailing list