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:54:00 UTC
[jira] [Commented] (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:comment-tabpanel&focusedCommentId=17285650#comment-17285650 ]
Daniel Sun commented on GROOVY-9946:
------------------------------------
[~paulk] According to your suggestion for PR1492, I've renamed annotation {{ConstantString}} to {{Pure}} and change its target elment to {{METHOD}}.
> 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 {
> private static final class Item {
> private int toStringInvocationCount = 0
> @Override
> @groovy.transform.Pure
> 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)