Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy @ symbol before fields

What does @ means before a field name in Groovy? For some classes I am able to access private fields that are not directly accessible, let's take ComposedClosure for example:

public class Person {   private String name }  def u = new Person(name:"Ron") println u.@name //Ron println u.name //Ron  a = {2} >> {3} println a.@first //first closure object println a.first //runtime error 
like image 656
Nutel Avatar asked Nov 29 '11 23:11

Nutel


People also ask

What is ?: In Groovy?

Yes, the "?:" operator will return the value to the left, if it is not null. Else, return the value to the right. "Yes, the "?:" operator will return the value to the left, if it is not null." - That is incorrect.

Does Groovy need semicolons?

No semicolons ​ semicolons are optional in Groovy, you can omit them, and it's more idiomatic to remove them.

Is def mandatory in Groovy?

Groovy Programming Fundamentals for Java Developers For variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement.


1 Answers

It allows you to override groovy's use of property accessors. If you write:

println u.name 

groovy will invoke the automatically generated getter Person.getName(). If you write:

println u.@name 

it will go directly to the field like it would in Java. In the case of the closure, it seems to have a first field but not a corresponding getFirst accessor.

In the groovy manual, it's documented as the direct field access operator.

like image 66
ataylor Avatar answered Oct 05 '22 22:10

ataylor