groovy - 当 GString 将更改其 toString 表示时

标签 groovy closures tostring assert gstring

我正在阅读 https://groovy-lang.org/closures.html#this 中的 Groovy 闭包文档.有一个关于 GString 行为的问题。

  1. Closures in GStrings


该文件提到了以下内容:

取以下代码:
def x = 1
def gs = "x = ${x}"
assert gs == 'x = 1'

该代码的行为与您预期的一样,但是如果您添加以下内容会发生什么:
x = 2
assert gs == 'x = 2'

你会看到断言失败了!有两个原因:

GString 只懒惰地评估值的 toString 表示

GString 中的语法 ${x} 不代表闭包,而是 $x 的表达式,在创建 GString 时计算。

在我们的示例中,GString 是使用引用 x 的表达式创建的。创建 GString 时,x 的值为 1,因此创建的 GString 值为 1。触发断言时,评估 GString,并使用 toString 将 1 转换为 String。当我们将 x 更改为 2 时,我们确实更改了 x 的值,但它是一个不同的对象,并且 GString 仍然引用旧的。

A GString will only change its toString representation if the values it references are mutating. If the references change, nothing will happen.



我的问题是关于上面引用的解释,在示例代码中,1显然是一个值,而不是引用类型,那么如果这个说法是真的,它应该在GString中更新为2吗?

下面列出的下一个例子我也觉得有点困惑(最后一部分)
为什么如果我们改变 Sam 将他的名字改为 Lucy,这一次 GString 是正确变异的??
我期待它不会变异??为什么两个例子中的行为如此不同?
class Person {
    String name
    String toString() { name }          
}

def sam = new Person(name:'Sam')        
def lucy = new Person(name:'Lucy')      
def p = sam                             
def gs = "Name: ${p}"                   
assert gs == 'Name: Sam'                
p = Lucy. //if we change p to Lucy                                
assert gs == 'Name: Sam'   // the string still evaluates to Sam because it was the value of p when the GString was created
/* I would expect below to be 'Name: Sam' as well 
 * if previous example is true. According to the     
 * explanation mentioned previously. 
 */         
sam.name = 'Lucy' // so if we mutate Sam to change his name to Lucy                  
assert gs == 'Name: Lucy'  // this time the GString is correctly mutated

为什么评论说'这次 GString 正确变异了?在之前的评论中,它刚刚提到

the string still evaluates to Sam because it was the value of p when the GString was created, the value of p is 'Sam' when the String was created



因此我认为它不应该在这里改变?
感谢您的帮助。

最佳答案

这两个示例解释了两个不同的用例。在第一个示例中,表达式 "x = ${x}"创建一个 GString内部存储的对象 strings = ['x = ']values = [1] .你可以检查这个特定的内部结构 GStringprintln gs.dump() :

<org.codehaus.groovy.runtime.GStringImpl@6aa798b strings=[x = , ] values=[1]>

两个对象,一个 String一在strings数组,和一个 Integer一在values数组是 不可变 . (值是不可变的,而不是数组。)当 x变量被赋予一个新值,它会在内存中创建一个与 1 无关的新对象。存储在 GString.values大批。 x = 2不是突变。这是新对象的创建。这不是特定于 Groovy 的事情,这就是 Java 的工作方式。您可以尝试以下纯 Java 示例来查看它是如何工作的:

List<Integer> list = new ArrayList<>();
Integer number = 2;
list.add(number);

number = 4;

System.out.println(list); // prints: [2]

带有 Person 的用例类(class)不一样。在这里你可以看到一个对象的变异是如何工作的。当你改变时sam.nameLucy ,你改变了存储在 GString.values 中的对象的内部阶段大批。相反,如果您创建一个新对象并将其分配给 sam变量(例如 sam = new Person(name:"Adam") ),它不会影响现有 GString 的内部结构目的。内部存储在 GString 中的对象没有变异。变量 sam在这种情况下,只是指内存中的不同对象。当您这样做时sam.name = "Lucy" ,你改变了内存中的对象,因此 GString (使用对同一对象的引用)看到了这种变化。它类似于以下纯 Java 用例:

List<List<Integer>> list2 = new ArrayList<>();

List<Integer> nested = new ArrayList<>();
nested.add(1);

list2.add(nested);
System.out.println(list2); // prints: [[1]]

nested.add(3);

System.out.println(list2); // prints: [[1,3]]

nested = new ArrayList<>();

System.out.println(list2); // prints: [[1,3]]

你可以看到 list2将对象的引用存储在 nested 所代表的内存中nested时的变量已添加到 list2 .当你变异时 nested通过向列表添加新数字,这些更改反射(reflect)在 list2 中。 ,因为你改变了内存中的一个对象 list2可以访问。但是当你覆盖 nested使用新列表,您创建一个新对象,然后 list2与内存中的这个新对象没有任何关系。您可以将整数添加到这个新 nested列表和 list2不会受到影响 - 它在内存中存储对不同对象的引用。 (之前可以使用 nested 变量引用的对象,但此引用稍后在代码中被新对象覆盖。)
GString在这种情况下,其行为类似于我上面向您展示的列表示例。如果您改变了内插对象的状态(例如 sam.name ,或将整数添加到 nested 列表中),则此更改会反射(reflect)在 GString.toString() 中在调用方法时产生一个字符串。 (创建的字符串使用存储在 values 内部数组中的值的当前状态。)另一方面,如果您用新对象覆盖变量(例如 x = 2sam = new Person(name:"Adam")nested = new ArrayList() ),它不会改变什么 GString.toString()方法产生,因为它仍然使用存储在内存中的一个(或多个)对象,并且之前与您分配给新对象的变量名称相关联。

关于groovy - 当 GString 将更改其 toString 表示时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61226559/

相关文章:

json - 如何调用以 json 字符串形式传递的函数

javascript - 修复循环中的闭包

java - 在 Allure2 中使用 @Step 注释时出现 NoSuchMethodError

groovy - Spock 框架可以进行对象深度比较吗?

java - 如何使谷歌数据存储在java中持久化

javascript - 标记模板字符串和闭包的问题

php - symfony2 ContextErrorException : Catchable Fatal Error: Object of class Proxies\__CG__\. ..\Entity\... 无法转换为字符串

java - JAVA 中的 toStrings 单元测试

java - 将 Int 值更改为字符串

groovy - Gradle:testDependents 和 testNeeded