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/16 07:43:00 UTC

[jira] [Updated] (GROOVY-9946) Add `@ConstantString` to mark constant result of `toString`

     [ https://issues.apache.org/jira/browse/GROOVY-9946?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Daniel Sun updated GROOVY-9946:
-------------------------------
    Summary: Add `@ConstantString` to mark constant result of `toString`  (was: Add annotation to mark constant result of `toString`)

> Add `@ConstantString` to mark constant result of `toString`
> -----------------------------------------------------------
>
>                 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
>
>
> In order to fully leverage the power of \{{GString}} caching, 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. 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)