[
https://issues.apache.org/jira/browse/GROOVY-11914?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King resolved GROOVY-11914.
--------------------------------
Fix Version/s: 6.0.0-alpha-1
Resolution: Fixed
> Add PurityChecker type checking extension
> -----------------------------------------
>
> Key: GROOVY-11914
> URL: https://issues.apache.org/jira/browse/GROOVY-11914
> Project: Groovy
> Issue Type: New Feature
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
> Fix For: 6.0.0-alpha-1
>
>
> I asked AI what features could be added to Groovy to give it vastly improved
> reasoning capabilities of Groovy code. First up it suggested @Modifies
> (GROOVY-11909) and the ModifiesChecker (GROOVY-11910). It said a close second
> would be a PurityChecker type checking extension. This issue looks at that.
> The key thing to note here is that this is an opt-in extension.
> If you use it, along with the existing @Pure annotation, it allows humans and
> AI to make assumptions about class or method behavior without reading the
> respective body.
> The checker doesn't provide fool-proof purity checking. It just covers the
> common cases.
> You may need to not enable the extension on code which falls outside those
> common cases.
> It just means that the human and/or AI will need to look inside the method
> body to reason about its purity.
> *Strict:*
> {code:groovy}
> @TypeChecked(extensions = 'groovy.typecheckers.PurityChecker')
> class MathUtils {
> @Pure
> int square(int x) { x * x } // OK
> @Pure
> long timestamp() { System.nanoTime() } // ERROR:
> non-deterministic
> @Pure
> int logged(int x) { println("x=$x"); x * 2 } // ERROR: logging
> }
> {code}
> ----
> *Allowing logging:*
> {code:groovy}
> @TypeChecked(extensions = 'groovy.typecheckers.PurityChecker(allows:
> "LOGGING")')
> class Service {
> @Pure
> int compute(int x) {
> println("computing $x") // OK: logging allowed
> return x * x
> }
> @Pure
> long getTime() { System.nanoTime() } // ERROR: non-deterministic (not
> allowed)
> }
> {code}
> ----
> *Allowing logging and non-determinism:*
> {code:groovy}
> @TypeChecked(extensions = 'groovy.typecheckers.PurityChecker(allows:
> "LOGGING|NONDETERMINISM")')
> class Diagnostics {
> @Pure
> String snapshot(Map state) {
> println("snapshot at ${System.nanoTime()}") // OK: both allowed
> return state.toString()
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)