I have some data source that requires wrapping operations in transactions, which have 2 possible outcomes: success and failure. This approach introduces quite a lot of boilerplate code. What I'd like to do is something like this (the same remains true for failures (something like @txFailure
maybe)):
@txSuccess(dataSource)
def writeData(data: Data*) {
dataSource.write(data)
}
Where @txSuccess
is a macro annotation that, after processing will result in this:
def writeData(data: Data*) {
val tx = dataSource.openTransaction()
dataSource.write(data)
tx.success()
tx.close()
}
As you can see, this approach can prove quite useful, since in this example 75% of code can be eliminated due to it being boilerplate.
Is that possible? If yes, can you give me a nudge in the right direction? If no, what can you recommend in order to achieve something like that?
It's definitely possible, but you don't necessarily need macros for the task.
Here's a simple solution, which doesn't use macros
And use it like