aspects on methods?

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 12 04:31:04 PDT 2016


On Tuesday, 12 July 2016 at 11:26:20 UTC, jj75607 wrote:
> I want to use aspect-like annotations to transform
>
>   @Lockable
>   class Abc
>   {
>       @sync
>       void f() {
>           writeln("f");
>       }
>
>       @shared
>       void g() {
>           writeln("g");
>       }
>   }
>
> to something like:
>
>   class Abc
>   {
>       shared(ReadWriteMutex) _lock123;
>
>       this()
>       {
>           _lock123 = cast(shared)(new ReadWriteMutex());
>       }
>
>       void f() {
>           synchronized((cast()_lock123).writer) {
>               writeln("f");
>           }
>       }
>
>       void g() {
>           synchronized((cast()_lock123).reader) {
>               writeln("g");
>           }
>       }
>   }
>
> How can I do that?

I don't think UDAs can be used that way in D, but I'm not an 
expert.
When I want to achieve something similar to this kind of 
rewriting, I usually use a mix of template mixins[1] and string 
mixins[2,3].

[1] https://dlang.org/spec/template-mixin.html
[2] https://dlang.org/spec/statement.html#MixinStatement
[3] https://dlang.org/mixin.html


More information about the Digitalmars-d-learn mailing list