[
https://issues.apache.org/jira/browse/GROOVY-11878?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18067444#comment-18067444
]
ASF GitHub Bot commented on GROOVY-11878:
-----------------------------------------
paulk-asert opened a new pull request, #2400:
URL: https://github.com/apache/groovy/pull/2400
There are two commits. The second commit contains some sample AST transforms
(see GROOVY-11878). They aren't meant to be production quality or included in
the merge of this PR, but are useful to check that things are working. In
particular:
* `@LoopInviant` would be moved to `groovy-contracts` and would probably use
the existing `@Invariant` (and maybe `@Ensures`) unless there are technical
reason why that doesn't pan out well when we get to the point of implementing
the real versions of such transforms
* `@Parallel` transform doesn't attempt to do any state sharing (bar the
loop variable) or protection, it's just a demo
> Allow AST transforms to be applicable in more places, initially loop
> statements
> -------------------------------------------------------------------------------
>
> Key: GROOVY-11878
> URL: https://issues.apache.org/jira/browse/GROOVY-11878
> Project: Groovy
> Issue Type: New Feature
> Reporter: Paul King
> Priority: Major
>
> The JVM doesn't support adding annotations in the bytecode for statements and
> expressions. That doesn't mean that Groovy couldn't support source-level AST
> transforms in those places. Code injection can happen in the normal way, and
> the transform will be "forgotten" after compilation.
> Examples might be:
> {code}
> @Parallel
> for (int i in 1..4) {
> println i ** 2
> }
> {code}
> which might have this output:
> {noformat}
> 1
> 16
> 9
> 4
> {noformat}
> Or these:
> {code}
> int sum = 0
> @Invariant({ 0 <= i && i <= 4 })
> for (int i in 0..4) {
> sum += i
> }
> assert sum == 10
> {code}
> {code}
> int i = 10
> @Invariant({ i >= 0 })
> @Ensures({ i > old.i }) // i decreases
> while (i > 0) {
> i = i - 1
> }
> assert i == 0
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)