Skip to content

Instantly share code, notes, and snippets.

@nobeans
Last active September 23, 2015 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobeans/619334 to your computer and use it in GitHub Desktop.
Save nobeans/619334 to your computer and use it in GitHub Desktop.
// g100pon #57 GStringが遅延評価ぽくなるケース
String s1 = "こんにちは"
String s2 = "g100pon"
def gstrFixed = "${s1}、${s2}です" // 文字列としてGStringに埋め込むのでこの時点で確定している
def gstrLazy = "${->s1}、${->s2}です" // Closureでくるむことで評価を遅延させる
assert gstrFixed.toString() == "こんにちは、g100ponです"
assert gstrLazy.toString() == "こんにちは、g100ponです"
s1 = "こんばんは"
s2 = "g1000bon"
assert gstrFixed.toString() == "こんにちは、g100ponです"
assert gstrLazy.toString() == "こんばんは、g1000bonです"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment