You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (Jira)" <ji...@apache.org> on 2021/02/17 06:32:00 UTC
[jira] [Updated] (GROOVY-9946) Add `@Pure` to mark constant result
of method
[ https://issues.apache.org/jira/browse/GROOVY-9946?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Daniel Sun updated GROOVY-9946:
-------------------------------
Summary: Add `@Pure` to mark constant result of method (was: Add `@ConstantString` to mark constant result of `toString`)
> Add `@Pure` to mark constant result of method
> ---------------------------------------------
>
> Key: GROOVY-9946
> URL: https://issues.apache.org/jira/browse/GROOVY-9946
> Project: Groovy
> Issue Type: Improvement
> Reporter: Daniel Sun
> Assignee: Daniel Sun
> Priority: Major
> Fix For: 4.0.0-alpha-3
>
> Time Spent: 50m
> Remaining Estimate: 0h
>
> In order to fully leverage the power of {{GString}} caching(GROOVY-9637), it would be good to have an annotaton to mark constant result of {{toString}}.
> Currently we could mark classes immutable, but it's too restricted for many cases. Currently we just cache literal of {{GString}} when its all values are immutable, but for more common cases, if the result of value's {{toString}} is constant, we could cache too. For example:
> {code:java}
> final class Test {
> @groovy.transform.KnownImmutable // actually the class is not immutable...
> private static final class Item {
> private int toStringInvocationCount = 0
> @Override
> synchronized String toString() {
> toStringInvocationCount++
> return "item"
> }
> }
> static void main(String[] args) {
> Item item = new Item()
> new StringBuilder().append("item: ${item}")
> System.out.println(item.toStringInvocationCount) // yield 1
> }
> }
> {code}
> *Better annotation:*
> {code:java}
> final class Test {
> @groovy.transform.ConstantString
> private static final class Item {
> private int toStringInvocationCount = 0
> @Override
> synchronized String toString() {
> toStringInvocationCount++
> return "item"
> }
> }
> static void main(String[] args) {
> Item item = new Item()
> new StringBuilder().append("item: ${item}")
> System.out.println(item.toStringInvocationCount) // yield 1
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)