You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2021/02/17 06:07:00 UTC
[jira] [Comment Edited] (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:comment-tabpanel&focusedCommentId=17285643#comment-17285643 ]
Paul King edited comment on GROOVY-9946 at 2/17/21, 6:06 AM:
-------------------------------------------------------------
We can certainly pursue generic solutions, since we can't have specific solutions for all cases. But, for the specific {{StringBuilder}} example, it might be better to add an {{append(StringBuilder, GString)}} DGM method. Such a DGM method would be complementary to the {{toString}} caching.
was (Author: paulk):
We can certainly pursue generic solutions, since we can't have specific solutions for all cases. But, for the specific {{StringBuilder}} example, it might be better to add an {{append(StringBuilder, GString)}} DGM method.
> 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
>
> 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)