Feeling Groovy again End of the Knowledge Worker?
Apr 27

New Groovy “use” method added by Sam

Groovy, Tech Add comments

Sam Pullara has added a cool new feature to Groovy. There is a new method use which allows you to introduce methods to existing class.

At the moment this works just for the duration of closure, but in the future you will be able to use it elsewhere.

This is pretty cool. I also would love to see:

  • Feature plugin/out: It would be great to use this mechanism to allow “features” of the language to be plugged in and out. It could enable backwards compatibility in the future, allowing changes to the language as experience shows us what to do (one worry wrt standardizing before a real impl is out there!)
  • Flush out AOP-isms: Mixin’s are great, but I would love to see a PointCut language, and rich support

From Sam’s Email:

excerpt from src/test/groovy/xml/dom/DOMTest.groovy:

xml = new StringReader(”<html><head><title class=’mytitle’>Test</title></head><body><p class=’mystyle’>This is a test.</p></body></html>”);
doc = DOMBuilder.parse(xml);
html = doc.documentElement;
use (groovy.xml.dom.DOMCategory) {
assert html.head.title.textContent == “Test”;
assert html.body.p.textContent == “This is a test.”;
assert html.find { it.tagName == “body” }.tagName == “body”;
assert html.getElementsByTagName(”*”).findAll { if (it["@class"] != “”) { return it } }.size() == 2;
}

One Response to “New Groovy “use” method added by Sam”

  1. wholesale replica handbags Says:

    thanks for your share!

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?

Loading...