What is the use of .@
in groovy? Can anyone explain me with a code snippet?
Have you seen the official documentation? It contains nice code samples.
Essentially, when you use normal .
operator, you access fields indirectly, using implicitly generated getters/setters. However, .@
allows you to access the field directly, skipping getter/setter.
This can be useful when you want to avoid some additional logic implemented in getter/setter and change the field directly. Violates tons of OOP principles, but the authors of Groovy found this construct to be useful.
That's the Java Field operator (according to the documentation)
There are examples in the documentation.
It is also used for accessing attributes when you are parsing XML (again, there's an example if you follow that link).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With