Add parameterized method to groovy class? - groovy

In groovy I can add a method to a class:
g.metaClass.bye = { println "Goodbye, $name" }
g.bye()
How can I add a method with parameters. Something like bye(int a, int b)?

Just add parameters to your closure
g.metaClass.bye = { int a, int b ->
a + b
}

Related

how do I override JUST the constructor in a Groovy class?

I would like to override ONLY the behavior of the constructor in a groovy class. Like so
class Foo {
def a
def b
Foo(int a, int b) {
this.a = a
this.b = b
println("I'm a Foo!")
}
}
class Bar extends Foo {
Bar(int a, int b) {
this.a = 2*a
this.b = 2*b
println("I'm a Bar!")
}
}
def f = new Foo(1, 2)
def b = new Bar(1, 2)
when I run this, the def f = new Foo(1, 2) line run successfully, but def bar = new Bar(1, 2) throws an exception:
I'm a Foo!
Caught: java.lang.NoSuchMethodError: Foo: method <init>()V not found
java.lang.NoSuchMethodError: Foo: method <init>()V not found
So I'm confused here. How do I override ONLY the constructor of a class property.
(incidentally, IntelliJ is complaining about the definition of the Bar constructor, saying 'There is no default constructor available in class Foo'), and that is confusing to me as well.
UPDATE:
I found that I could solve the problem by adding a no-arg constructor to Foo as Foo() {}. And that seems to solve the problem with Bar. But I still have no idea what's going on here.
groovy based on java and in java you can't override constructor of ancestor. you can only extend it.
if you are not specifying which constructor to extend by using super(a, b) (for example) - then compiler will try to use default parent constructor super() - and here you got an original error.
java.lang.NoSuchMethodError: Foo: method ()V not found
so, this one should work:
class Foo {
def a
def b
String toString(){ "${this.getClass()}[$a, $b]" }
Foo(int a, int b) {
this.a = a
this.b = b
println("I'm a Foo!")
}
}
class Bar extends Foo {
Bar(int a, int b) {
super(a, b) // <<-- the only change
this.a = 2*a
this.b = 2*b
println("I'm a Bar!")
}
}
def f = new Foo(1, 2)
def b = new Bar(1, 2)
println f
println b
result:
I'm a Foo!
I'm a Foo! <<-- this is coming from Bar (inherited from Foo)
I'm a Bar!
class Foo[1, 2]
class Bar[2, 4]

Why can't you modify closure parameters of inline methods?

I've got this section of code:
class Main {
static inline function difference(a:Int, b:Int, ?f:(Int, Int) -> Int):Int {
if (f == null) {
f = (a, b) -> a - b;
}
return f(a, b);
}
static function main() {
trace(difference(42, 37));
trace(difference(42, 37, (a, b) -> a - b));
}
}
Which, when I compile using haxe --main Main, fails with this error:
Main.hx:11: characters 15-50 : Cannot modify a closure parameter inside inline method
Main.hx:11: characters 15-50 : For function argument 'v'
If I change Main.difference to not be inline, this error doesn't come up and everything compiles fine.
Why does this error occur?
Edit: I've found out I can also assign the argument to a variable first, and then pass the variable to Main.difference, like this:
static function main() {
var f = (a, b) -> a - b;
trace(difference(42, 37, f));
}
Which works fine with Main.difference being inlined. How does assigning the function to a variable first change things though?
This is related to how inline functions are unwrapped by the compiler. Let us take a simpler variant of your code:
class HelloWorld {
static inline function difference(a:Int, b:Int, ?f:(Int, Int) -> Int):Int {
return f(a, b);
}
static function main() {
trace(difference(42, 37, (a, b) -> a - b));
}
}
When disabling optimizations, this will yield the following JavaScript:
HelloWorld.main = function() {
console.log("HelloWorld.hx:14:",(function(a,b) {
return a - b;
})(42,37));
};
So the body of difference has been incorporated into main using a JavaScript closure. My best guess for what is happnening in your exact case is something like this:
HelloWorld.main = function() {
var v = function(a,b) {
return a - b;
}
console.log("HelloWorld.hx:14:", (function(a,b) {
if (v == null) {
v = function(a, b) {
return a - b;
}
}
return v(a, b);
})(42, 37));
};
This alters the value of v, which exists outside of difference, which has been automatically placed there as a binding for the anonymous lambda. This is what the compiler is trying to avoid. This would not be the end of the world in your case, but in general this is bad and would lead to issues in many programs.
There is a way to inline this code perfectly by hand without this, but I think that there is some weirdness surrounding how annonymous lambdas are currently handled. The situation may improve in the future.
When you explicitly defined f in main, the compiler is intelligent enough to rename the nested f as f1, which is why the issue does not occur:
HelloWorld.main = function() {
var f = function(a,b) {
return a - b;
};
var f1 = f;
if(f1 == null) {
f1 = function(a,b) {
return a - b;
};
}
console.log("HelloWorld.hx:14:",f1(42,37));
};
But this would also work if the inline part of this function is important to you:
class HelloWorld {
static inline function difference(a:Int, b:Int, ?f:(Int, Int) -> Int):Int {
var h = f;
if (h == null) {
h = (a, b) -> a - b;
}
return h(a, b);
}
static function main() {
trace(difference(42, 37, (a, b) -> a - b));
}
}

How can a Groovy closure curry itself for repetition?

I'm implementing Groovy step definitions for Cucumber-JVM and I want a step to be able store itself so that the next step can repeat it n times.
Given(~'the (\\S+) is in the blender') { String thing ->
// do stuff...
context.repeatable = self.curry(thing)
}
What should "self" be in the above code?
I can't use "this" as that refers to the enclosing object (whatever that is in this case, maybe the script).
Since curry is a method of the Closure class, directly invoking curry applies to the closure, both if it is named:
def context
test = { String thing ->
context = curry(thing)
thing.toUpperCase()
}
test('hello')
println context.call()
test('world')
println context.call()
=>
HELLO
WORLD
or anonymous:
def context
['test'].each { String thing ->
context = curry(thing)
thing.toUpperCase()
}
println context.call()
=>
TEST
You can try using unreferenced curry method passing the received parameters:
clos = { String text ->
if (text) {
println "text=$text"
a = curry null
a()
} else {
println "done"
}
}
clos "closure text"
Will print:
text=closure text
done
Update
You can also use clone():
closure = {
def clone = clone()
}

How to select specific field using Groovy Closure

Given:
class FruitBasket {
int apples = 0
int oranges = 0
}
I need to pick out apples from each FruitBasket. The work need to be done in processFruit:
def processFruit(list, picker) {
list.each {
println "processing " + picker(it)
}
}
def processAll() {
List fruitList = [
new FruitBasket("apples": 2, "oranges": 4),
new FruitBasket("apples": 3, "oranges": 5)
]
processFruit(fruitList, applePicker)
}
def applePicker(FruitBasket f) {
return f.getApples()
}
but it is complaining # runtime that
No such property: applePicker for class: FooTest
possibly a problem with the closures FruitBasket arg...
In that code, applePicker is a method, not a closure.
You can either use a method handle to pass the method as a parameter like so:
processFruit(fruitList, this.&applePicker)
Or change it to an actual closure:
def applePicker = { FruitBasket f -> return f.getApples()    }
You are passing applePicker to processFruit, but it is a method. You can only pass closures this way. Redefine applePicker as a closure like so:
applePicker = { FruitBasket f ->
return f.getApples()
}
Or convert the method to a closure when processFruit is called:
processFruit(fruitList, this.&applePicker)

Dynamically add a property or method to an object in groovy

Is it possible to add a property or a method to an object dynamically in Groovy? This is what I have tried so far:
class Greet {
def name
Greet(who) { name = who[0].toUpperCase() + [1..-1] }
def salute() { println "Hello $name!" }
}
g = new Greet('world') // create object
g.salute() // Output "Hello World!"
g.bye = { println "Goodbye, $name" }
g.bye()
But I get the following exception:
Hello World!
Caught: groovy.lang.MissingPropertyException: No such property: bye for class: Greet
Possible solutions: name
at test.run(greet.groovy:11)
If you just want to add the bye() method to the single instance g of the class Greet, you need to do:
g.metaClass.bye = { println "Goodbye, $name" }
g.bye()
Otherwise, to add bye() to all instance of Greet (from now on), call
Greet.metaClass.bye = { println "Goodbye, $name" }
But you'd need to do this before you create an instance of the Greet class
Here is a page on the per-instance metaClass
And here is the page on MetaClasses in general
Also, there's a bug in your constructor. You're missing who from infront of your [1..-1] and if the constructor is passed a String of less than 2 characters in length, it will throw an exception
A better version might be:
Greet( String who ) {
name = who.inject( '' ) { String s, String c ->
s += s ? c.toLowerCase() : c.toUpperCase()
}
}
As metioned in the comments,
Greet( String who ) {
name = who.capitalize()
}
is the proper way

Resources