BenHuman
BenHuman

Reputation: 175

Why does ":" at the last of a groovy statement does not throw any error?

I mistakenly wrote the following in the groovy console but afterwards I realized that it should throw error but it did not. What is the reason behind groovy not throwing any error for colon at last of the statement?Is it allocated for documentation or sth like that?

    a:
    String a
    println a

This threw no error when i tried executing this code in https://groovyconsole.appspot.com/

Upvotes: 1

Views: 152

Answers (2)

injecteer
injecteer

Reputation: 20707

One good use of labels in Groovy I can think of is the Spock Framework, where they are used for clauses:

def 'test emailToNamespace'() {
  given:
  Partner.metaClass.'static'.countByNamespaceLike = { count }

  expect:
  Partner.emailToNamespace( email ) == res

  where:
  email                                      |  res                       | count
  'aaa.com'                                  |  'com.aaa'                 | 0
  'aaa.com'                                  |  'com.aaa1'                | 1
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503649

It's a label, just like it would be in Java. For example:

a:
for (int i = 0; i < 10; i++)
{
    String a = "hello"
    println a
​    break a; // This refers to the label before the loop
}​

Upvotes: 5

Related Questions