I'm not talking about mimicking AOP features in Scala (i.e. using Traits instead of Aspects), I'm wondering if it is possible to do true AOP in Scala (i.e. advices, aspects, joint points, weaving etc ...)
-
Aren't advices, join-points and weaving the main _problems_ of AOP and soruces of it's complexity? Whereas traits let you achieve similar effects but in a simpler, more predictable way, with no things that will only come up on runtime? – Michal M Mar 07 '17 at 12:28
-
the Macwire DI framework has interceptor support https://github.com/adamw/macwire#interceptors which is similar to annotation based AOP. – WeiChing 林煒清 Feb 14 '18 at 12:36
2 Answers
Mixin has been the classic way to introduce AOP in Scala (as in "AOP-style Mixin Composition Stacks in Scala" from Jonas Bonér).
But I know only of "Method Proxy-Based AOP in Scala" (Daniel Spiewak -- also here on SO -- and Tian Zhao) as an advanced AOP implementation in Scala (source code here).
Our technique uses Scala’s higher-order functions to intercept method calls with minimal syntactic overhead imposed on the base program.
This framework allows developers to define pointcuts by specifying class types and method signatures. The framework also allows access to context variables, while aspects can insert advice code before or after the advised body.
We have had no problems using Spring AOP with our Scala classes. It's not making any bytecode modifications though (only proxying).

- 3,243
- 1
- 19
- 20