Groovy字符串计算运行时 [英] Groovy String evaluation runtime

查看:185
本文介绍了Groovy字符串计算运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 静态值=''
static construct =$ { - > value - '/'}

因此,当我 =/www.stackoverflow.com时,构造等于www.stackoverflow.com



但是当我做

  static value =''
static construct = { - > value - '/'}

等于某些关闭名称。我想知道这是什么目的?为什么要使用closure,GString的一切都ok了?



编辑:
该值从此方法调用中更改。

  def someCoplmexMethod(){
value =/ www.stackoverflow.com
}


解决方案

简而言之$ { - > value - /'c



首先来澄清一些事情... value是一个值为value的字符串。 $ value是一个GString,等于value的值为String。所以如果值是lol.com,那么这将是GString等于的值。



重要注意事项是两件事,GString的值被评估为惰性,但是用于此的对象不是。因此,如果在$ value value是一个类与toString()方法,每次将返回一个不同的字符串,那么GString的toString时间。当然,这不是String对象的情况。所以,如果你有例如像

  def value = 1 
def gstring =$ value
value = 2
assert gstring ==1

2



{ - > value} 现在是一个打开的块,将返回值。评价是懒惰的,因此每次。因此在

  def value = 1 
def closure = { - > value}
def a = closure()
value = 2
def b = closure()
assert a == 1
assert b == 2

那么a将获得值1,b的值为2.方法调用类表达式 closure()将调用打开块。



最后$ { - > value - '/'}被视为一个GString一个开放的块。 GString将在每次调用toString()方法时像打开块一样来评估打开的块,但是这将导致打开块的调用,因此再次评估值本身。其结果是:

  def value = 1 
def gstring =$ { - > value}
value = 2
assert gstring ==2

与简单的GString版本,那么你会看到结果改变。一个更复杂的例子:

  def i = 1 
def gstring =$ { - > i ++}
assert gstring ==1
assert i == 2
assert gstring ==2
assert i == 3
assert gstring ==3
assert i == 4

正如你所看到的,toString gstring每次更改时都会更改,因为打开块。


Hi I have a String value that is populated runtime and I want to use it to construct another String.

static value= ''
static construct = "${-> value - '/'}"

So when I value = "/www.stackoverflow.com" , construct is equal to "www.stackoverflow.com"

but when I do

static value= ''
static construct = {-> value - '/'}

construct is equals to some closure name. I am wondering what is the purpose of this? Why using closure,GString everything is ok? And why when use only closure nothing happens?

Edited: The value is changed from this method call.

def someCoplmexMethod(){
   value="/www.stackoverflow.com"
}

解决方案

In short "${-> value - '/'}" is a fully lazy evaluated GString for value.

First to clarify some things... "value" is a String with the value "value". "$value" is a GString equal to the value of value as String. So if value is "lol.com", then this will be the value the GString is equal to as well.

Important to notice are two things here, the value of the GString is evaluated lazy, but the objects used for this are not. So if in "$value" value is a class with a toString() method, that will return a different String each time, then the GString's toString() would be different each time. Of course that is not the case for String objects. So if you for example have code like

def value=1
def gstring="$value"
value=2
assert gstring=="1"

then the gstring will still be "2".

{->value} is now a open block, that will return value. The evaluation is lazy and thus each time. So in

def value=1
def closure={->value}
def a=closure()
value=2
def b=closure()
assert a==1
assert b==2

then a will get the value 1 and b the value 2. The method-call-like expression closure() will call the open block.

Finally "${-> value - '/'}" is to be seen as a GString containing an open block. The GString will evaluate the open block each time it's toString() method is called like usual, but this time this will result in the call of the open block and thus evaluate value itself again. The result is:

def value=1
def gstring="${-> value}"
value=2
assert gstring=="2"

If you compare the example with the simple GString version, then you will see that the result changed. A more complex example:

def i=1
def gstring="${-> i++}"
assert gstring=="1"
assert i==2
assert gstring=="2"
assert i==3
assert gstring=="3"
assert i==4

As you can see, the toString()-value of gstring changes every time it is evaluated, because of the open block.

这篇关于Groovy字符串计算运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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