Created
August 20, 2012 12:32
-
-
Save irof/3403702 to your computer and use it in GitHub Desktop.
GStringの評価が遅延されてるように見えるケースの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// プログラミングGROOVY P69 | |
list = ['x'] | |
def gs = "$list ${list[0]}" | |
assert gs instanceof GString | |
assert gs == '[x] x' | |
list[0]='y' | |
assert gs == '[y] x' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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