Groovy GStringImpl和String行为 [英] Groovy GStringImpl and String behaviour

查看:751
本文介绍了Groovy GStringImpl和String行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读有关Groovy集合中 GStringImpl s和 String s的行为。



我明白这个计算结果为false的原因...

$ {'test'}。equals(test)== false



是由于 .equals()合同,但是我想知道是否有一个原因 GStringImpl 不能被评估为 String 立即。所以当我做这样的事情时......



$ {'someString'}



我没有得到 GStringImpl ,我只是得到一个普通的Java String

我知道有一些解决方法,例如

String s =$ {'someString'}



然而像这样的东西有点不方便,而且对于Groovy新手来说, GStringImpl String 似乎是一个很大的陷阱。因为某些原因,GStrings 并不立即被评估为String,主要与懒惰评估有关( 这对记录非常有用)和模板。在字符串和GString 中,您可以找到一个很好的解释:



< blockquote>

GString 可能涉及延迟评估,因此直到调用toString()
方法才评估GString
即可。这个懒惰的评估
对于日志记录很有用,因为它允许计算
字符串,对值赋值toString(),并且可以完成不同字符串的连接
懒惰,如果有的话。



当你不想使用模板引擎,
或者当你真的想完全懒惰的评估GStrings时,GString非常方便。当嵌入GString中的
变量时,会在
变量上调用toString()以获取字符串表示形式,并将其插入到
最终字符串中。


因此:


GString和String是两个不同的类,因此使用GString
对象作为Map对象的键或涉及GString
对象的比较,可以在与String
对象组合时产生意外的结果,因为GString和String不会具有相同的hashCode,也不会
是否相等。在两个
类型之间没有用于比较或映射键的自动强制,所以有时需要
显式地在GString对象上调用toString()。



如果代码是
期望GString,意外转换为String可能会导致问题,就像groovy.sql类中的方法一样。



I've recently been reading about the behaviour of GStringImpls vs Strings when used in collections in Groovy.

I understand that the reason this evaluates to false...

"${'test'}".equals("test") == false

is due to the symmetry requirement of the .equals() contract, however I was wondering if there was a reason the GStringImpl couldn't just be evaluated to a String immediately. So when I do something like this...

"${'someString'}"

I don't get a GStringImpl, I just get a plain Java String back, which I can immediately use as the key in a map, for example.

I know there are some workarounds, like

String s = "${'someString'}"

however stuff like this is a bit inconvenient, and the mixup between GStringImpl and String seems to be a big 'gotcha' for Groovy newbees.

解决方案

GStrings are not evaluated inmediately to String because of some reasons, mainly related to lazy evaluation (which is quite good for logging) and templating. In Strings and GString you can find a good explanation:

GString can involve lazy evaluation so it's not until the toString() method is invoked that the GString is evaluated. This lazy evaluation is useful for things like logging as it allows the calculation of the string, the calls to toString() on the values, and the concatenation of the different strings to be done lazily if at all.

GString is pretty handy when you don't want to use a template engine, or when you really want full lazy evaluation of GStrings. When some variable embedded in a GString, the toString() is called on that variable to get a string representation, and it's inserted into the final string.

Therefore:

GString and String are two distinct classes, and hence use of GString objects as keys for Map objects or comparisons involving GString objects, can produce unexpected results when combined with String objects since a GString and a String won't have the same hashCode nor will they be equal. There is no automatic coercion between the two types for comparisons or map keys, so it's sometimes necessary to explicitly invoke toString() on GString objects.

Unexpected conversion to String can lead to problems when code is expecting a GString, as for methods in groovy.sql classes.

这篇关于Groovy GStringImpl和String行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆