Skip to content

Instantly share code, notes, and snippets.

@irof
Created August 20, 2012 12:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irof/3403702 to your computer and use it in GitHub Desktop.
Save irof/3403702 to your computer and use it in GitHub Desktop.
GStringの評価が遅延されてるように見えるケースの
// プログラミングGROOVY P69
list = ['x']
def gs = "$list ${list[0]}"
assert gs instanceof GString
assert gs == '[x] x'
list[0]='y'
assert gs == '[y] x'
list = ['x', ['a']]
def gs = "$list ${list[0]} ${list[1]}"
assert gs instanceof GString
assert gs == '[x, [a]] x [a]'
list[0]='y'
assert gs == '[y, [a]] x [a]'
list[1]<<'a'
assert gs == '[y, [a, a]] x [a, a]'
list[1]=['b']
assert gs == '[y, [b]] x [a, a]'
list = ['x']
def gs = "$list ${list[0]}"
assert gs instanceof GString
assert gs == '[x] x'
list[0]='y'
assert gs == '[y] x'
list.add(0, 'z')
assert gs == '[z, y] x'
list=['a']
assert gs == '[z, y] x'
list = ['x']
def gs = "${list} ${list.toString()} ${list[0]}"
assert gs instanceof GString
assert gs == '[x] [x] x'
list[0]='y'
assert gs == '[y] [x] x'
class Hoge {
String value
public String toString() {
println 'hoge:' + value
value
}
}
def hoge1 = new Hoge()
hoge1.value='hoge1'
def hoge2 = new Hoge()
hoge2.value='hoge2'
def gs = "$hoge1 ${hoge2.toString()}"
hoge1.value='hoge11'
hoge2.value='hoge22'
println '-' * 20
println gs
println '-' * 20
/* ==== 実行結果 ====
hoge:hoge2
--------------------
hoge:hoge11
hoge11 hoge2
--------------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment