Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do method aliasing in groovy?

I am trying to redefine dynamic methods of a domain in groovy. Is there something similar alias method in ruby in groovy?

like image 254
prabhap Avatar asked Oct 26 '10 04:10

prabhap


People also ask

How do you call a method in Groovy?

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.

Is method in Groovy?

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.

What def means in Groovy?

2. Meaning of the def Keyword. The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language.

What is alias name in Java?

In Java, Alias is used when reference, which is of more than one, is linked to the same object. The issue with aliasing is when a user writes to a particular object, and the owner for the several other references do not expect that object to change.


1 Answers

Do you mean like the method reference operator .& ?

def out = System.out.&println
out << "Hello"

and

def greet(name) {
    println "Hello $name"
}

def sayHello = this.&greet

sayHello "Ronny"

It is mentioned at http://groovy.codehaus.org/Operators but an example is missing

like image 173
rlovtang Avatar answered Oct 12 '22 15:10

rlovtang