Sök…


Syntax

  • $
  • $ {}
  • $ {->}

Grundläggande

def str = 'nice'
assert "Groovy is $str" == 'Groovy is nice'

Prickigt uttryck

def arg = [phrase: 'interpolated']
assert "This is $arg.phrase" == 'This is interpolated'

Ivrig uttryck

def str = 'old'
def interpolated = "I am the ${str} value"
assert interpolated == 'I am the old value'
str = 'new'
assert interpolated == 'I am the old value'

Lat uttryck

Vi kan ha lata interpolering i strängar. Detta är annorlunda än normal interpolering eftersom GString kan ha olika värden, beroende på stängningen, när den omvandlas till en sträng.

def str = 'old'
def interpolated = "I am the ${ -> str} value"
assert interpolated == 'I am the old value'
str = 'new'
assert interpolated == 'I am the new value'

Uttryck

def str = 'dsl'
def interpolated = "Groovy ${str.length() + 1} easy ${str.toUpperCase()}"
assert interpolated == 'Groovy 4 easy DSL'
str = 'Domain specific language'
assert interpolated == 'Groovy 4 easy DSL'


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow