geb - What does curly brackets syntax mean in Groovy? -
what syntax mean in groovy:
class createmessagepage extends page { static @ = { assert title == 'messages : create'; true } static url = 'messages/form' static content = { submit { $('input[type=submit]') } myverystrangeform { $('form') } errors(required:false) { $('label.error, .alert-error')?.text() } } }
(taken spring mvc test htmlunit manual)
the question groovy , know answer in groovy terms.
what content
? static variable? it's name random or predefined base class of page
?
what =
(equal singn) after it? assignment operator?
what @ right hand side of =
? closure? or if anonymous class? or if these same?
what submit
inside curly braces?
is variable? why there no assignment operator after then?
is function definition? can define functions in arbitrary places in groovy? if function definition, errors
then?
is submit
is function call, receiving { $('input[type=submit]') }
parameter? if yes, function can defined? example, myverystrangeform
defined (is nowhere)?
if function call, won't work since undefined...
quick answer questions: it's block of code, anonymous function, called closure in groovy.
see http://www.groovy-lang.org/closures.html
in groovy can reference/pass/set such closure, in functional language.
so this:
static @ = { assert title == 'messages : create'; true }
means class field at
set closure (notice, not result of closure execution, closure itself, block of code). type of @ omitted there, static def at
or static object at
, or static closure at
this code executed anytime later, in different context, title
defined, etc.
this:
submit { $('input[type=submit]') }
means calling function submit
closure argument.
if want write own function this, should like:
def submit(closure code) { code.call() }
brackets omitted, written submit({$('input[type=submit]')})
. same other function well, println 'hello world!'
instead of println('hello world
).
there's common practice define closure last argument, like:
def errors(map opts, closure code) { .... }
at case pass first arguments usual, wrapped in brackets, , closure outside:
errors(required:false) { ...... }
same to:
errors([required: false], { ..... })
Comments
Post a Comment