Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of "use" keyword/method in groovy?

Tags:

I read use keyword in Groovy. But could not come out with, for what it has been exactly been used. And i also come with category classes, under this topic,what is that too? And from, Groovy In Action

class StringCalculationCategory {   static def plus(String self, String operand) {     try {       return self.toInteger() + operand.toInteger()     } catch (NumberFormatException fallback) {       return (self << operand).toString()     }   } }  use (StringCalculationCategory) {   assert 1 == '1' + '0'   assert 2 == '1' + '1'   assert 'x1' == 'x' + '1' } 

With the above code, can anyone say what is the use of use keyword in groovy? And also what the above code does?

like image 540
Ant's Avatar asked Mar 22 '11 16:03

Ant's


People also ask

What are keywords in Groovy?

The three keywords it , delegate , and owner are used with Groovy closures. They provide handles to implicitly available objects from within the closure's body.

How do I use Groovy methods?

In Groovy, we can add a method named call to a class and then invoke the method without using the name call . We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: () . This can be especially useful in for example a DSL written with Groovy.

What is the use of def in Groovy?

The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language. Here, firstName will be a String, and listOfCountries will be an ArrayList. Here, multiply can return any type of object, depending on the parameters we pass to it.

How do you write a method in Groovy script?

A method is in Groovy is defined with a return type or with the def keyword. Methods can receive any number of arguments. It's not necessary that the types are explicitly defined when defining the arguments. Modifiers such as public, private and protected can be added.


2 Answers

See the Pimp My Library Pattern for what use does.

In your case it overloads the String.add(something) operator. If both Strings can be used as integers (toInteger() doesn't throw an exception), it returns the sum of those two numbers, otherwise it returns the concatenation of the Strings.

like image 145
Adam Avatar answered Oct 24 '22 05:10

Adam


use is useful if you have a class you don't have the source code for (eg in a library) and you want to add new methods to that class.

By the way, this post in Dustin Marx's blog Inspired by Actual Events states:

The use "keyword" is actually NOT a keyword, but is a method on Groovy's GDK extension of the Object class and is provided via Object.use(Category, Closure). There are numerous other methods provided on the Groovy GDK Object that provide convenient access to functionality and might appear like language keywords or functions because they don't need an object's name to proceed them. I tend not to use variables in my Groovy scripts with these names (such as is, println, and sleep) to avoid potential readability issues.

There are other similar "keywords" that are actually methods of the Object class, such as with. The Groovy JDK documentation has a list of such methods.

like image 29
Simon Tewsi Avatar answered Oct 24 '22 04:10

Simon Tewsi