2009-04-29 16:37A bit of help with closures in GroovyWhile looking through my ideas for potential future blog posts, I came across some notes I made while trying to consolidate my understanding of closures in Groovy. I also had a few points I wanted to make about other aspects of the language, including things I found while working on the game that I presented in my last blog post, so I thought that this would be a good time to write up these ideas. This post may end up sounding like a criticism of Groovy’s documentation and tool support, but it is a fairly new language and I think that should be taken into account. The small problems I have come across have certainly not put me off it, but have perhaps given me a greater appreciation of more mature languages like PHP. Beginning closuresThe first thing to do is to read the guide to closures in Groovy’s documentation. If you can read it all the way from top to bottom in one sitting without your head hurting, you’re doing it wrong. What you should find is that you reach something like: def f = { list, value -> list << value }
x = [] f(x, 1) f(x, 2,) //trailing comma in argument list OK f(x, 3) assert x == [1, 2, 3] and your mind starts frantically trying to build parsers and execute the code until the Once you’ve got your head around that, you should be able to get up to this bit in the documentation: def f= {m, i, j-> i + j + m.x + m.y }
assert f(6, x:4, y:3, 7) == 20 You might start to wonder where the m comes from and worry that i and j aren’t defined, but don’t worry, it does all make sense. What is happening is, the arguments to Silent failI did mention in my earlier post that it was difficult getting Swing to do what you wanted in Groovy. A good example of how opaque the technology can be is to look at the documentation page for Take for example the challenge of trying to set the appearance of your program’s graphical interface. Not only does Groovy assign defaults for you, like a grey background, but it then ignores things you specify like The worst example, though, is when this conspiracy of silence overlaps with Groovy trying to be too clever. I present two lines of code which should almost be a test case for checking whether a language or execution environment is designed to help the programmer: println "1"*3
println 3*"1" If you thought that either of those lines would produce the output “3” then you have a lot of exciting discoveries to look forward to when you start using Groovy. Also, if you thought that the two lines would produce the same output, then you might want to spend some time multiplying matrices until this non-commutativity becomes intuitive. Finally, if you thought that the first line would produce “111” and the second line would produce an error message you are half right, because the second line, instead of doing something useful, does nothing at all, not even give you an error message to tell you you’ve made a mistake. BugI could try running this code in the latest version of the groovyConsole to check that this behaviour is still present, but after the last upgrade I did I now get this error message when I try to load it:
foo@bar$ groovyConsole
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caused by: java.lang.UnsatisfiedLinkError: Can’t load library: /usr/lib/jvm/java-6-openjdk/jre/lib/ext/libjava-access-bridge-jni.so
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1666)
at java.lang.Runtime.load0(Runtime.java:787)
at java.lang.System.load(System.java:1022)
at org.GNOME.Accessibility.JavaBridge. Does this mean I should be using Scala instead? Trackbacks
Trackback specific URI for this entry
No Trackbacks
|
QuicksearchCategoriesSyndicate This BlogBlog Administration |