Considering Groovy for your next big project? We did. All things considered, it figured to be a safe choice given its Java pedigree. From the limited exposure I'd had to Groovy up until that point, it looked and felt remarkably familiar to Ruby (a good thing). I had even heard you could cut & paste any amount of Java into a .groovy file and it would just work. Depending on what 'work' actually means, this is mostly true.
One of the small untruths about Groovy behaving 'just like Java' is worth serious consideration - especially if you want your .groovy code to become API-ready.
By API-ready, I mean:
- Thoroughly documented
- Safe to use
- Intuitive
- If at all possible, preserves backwards compatibility when making API changes
def s = "12" def chars = s.value s.value = "ABCD" assert !chars.is(s.value) assert s == "AB" s.count=3 assert s == "ABC" s.offset = 1 assert s == "BCD" def s2 = "0123" s = s2.substring(0,2) assert s.value.is(s2.value) s2.value = "ABCD" assert s2 == "ABCD" assert s == "01"
Kind of reminds me of the former U.S. President Bill Clinton's grand court testimony where he perjures himself while arguing the exact meaning of the word "is". Bill split hairs - here, we seem to be splitting Strings.
public class A {
private int member = 20; private String method() { return "hello"; } def publicMethod (String name_){ def localVar = member + 5; def localVar2 = "Parameter: ${name_}"; return { println "${member} ${name_} ${localVar} ${localVar2} ${method()}" } } } A sample = new A(); def closureVar = sample.publicMethod("Xavier"); closureVar();
The above code will print:
20 Xavier 25 Parameter: Xavier hello
For this to work, the Groovy compiler and Meta Object Protocol (MOP) impl must have access to private members at both compile-time and runtime, respectively. Remember, Groovy compiles to .class files and runs in the JRE like any other Java.
Comments