aspects on methods?

Anonymouse via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 05:58:12 PDT 2016


On Tuesday, 12 July 2016 at 11:26:20 UTC, jj75607 wrote:
> I want to use aspect-like annotations to transform
[...]

Two methods spring to mind but both create new types.

You can either write a function that iterates through the members 
of your class, generating a string mixin that selectively hijacks 
calls to functions you have annotated with some UDA. Then it 
would just forward the function call inside synchronized blocks. 
alias this should hopefully be enough to forward other members.

See https://dpaste.dzfl.pl/6a9cc4946caf for a rough example. It's 
not robust, doesn't work with overloads (needs an additional 
foreach over __traits(getOverloads, C, memberstring)), but I hope 
it conveys the idea.

The alternative is to write a wrapper that similarly hijacks and 
wraps calls inside synchronized blocks, but by using opDispatch. 
The approach is less complex but sadly doesn't work as well; 
alias this seems to be more or less mutually exclusive to 
opDispatch, which makes sense. See 
https://dpaste.dzfl.pl/a5dac71ab4f8. Accessing enums and other 
types is also iffy since functions can't return types. As long as 
you make the base class instance public you can always bypass 
things that way.

In both examples it may make sense to have the generated 
functions return via auto ref, it depends on your use cases.


More information about the Digitalmars-d-learn mailing list