The document summarizes Darren Cruse's presentation on Groovy as a dynamic language and metaprogramming examples in Groovy. The presentation covers whether Groovy has arrived as a language, characteristics of dynamic languages like Groovy, examples of adding new methods to Groovy classes to emulate Ruby-style object creation, and automatically generating UML diagrams from Groovy scripts. Slides include surveys of Groovy usage, definitions of dynamic programming languages, examples of dynamic languages, comparisons of static versus dynamic compilation, and code samples for adding factory methods in Groovy.
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
Groovy as a Dynamic Language
1. Groovy as a
Dynamic Language
Darren Cruse
(with just a smidge of
metaprogramming)
Wednesday, April 22, 15
2. Agenda
• Has Groovy arrived?
(what do you guys think?)
• Groovy as a “Dynamic Language”
(groovy as part of the family of languages that includes
smalltalk, perl, javascript, ruby, etc)
• Some metaprogramming code examples:
Adding “Ruby style” new methods to groovy
(if time brief look at): Auto-generating UML sequence
diagrams from running a groovy script.
Wednesday, April 22, 15
3. Survey 1
Which is more important to your
company/projects:
a. Groovy as a language
b. Grails as a web framework
c. Both are equally important
Wednesday, April 22, 15
4. Survey 2
At my company we are using groovy:
a. Heavily as the primary language for a project.
b. Moderately as a complement to java.
c. Lightly for unit tests, throw away scripts, etc.
d. We are Evaluating groovy for possible future use.
e. We are Not Using Groovy.
Wednesday, April 22, 15
8. Ease of Adoption
(versus)
Benefits of Adoption
“Ease of
Adoption”
“Benefits of
Adoption”
“Sweet spot of
innovation”
Adapted From: The Myths of Innovation, by Scott Berkun
Wednesday, April 22, 15
10. What is a
“Dynamic Language”?
(Graeme Rocher and Bob Lee, from 2006)
http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html
Wednesday, April 22, 15
11. What is a
“Dynamic Language”?
(Graeme Rocher and Bob Lee, from 2006)
http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html
Wednesday, April 22, 15
12. “Dynamic Programming Language”
(according to Wikipedia)
“Dynamic Programming Language describes a class of high
level programming languages that execute at runtime
many common behaviors that other languages might
perform at compilation.”
Wednesday, April 22, 15
13. “Dynamic Programming Language”
(according to Wikipedia)
Most Dynamic Languages:
• Are Dynamically Typed
• Have functions as “first class citizens”
• Allow modifications to types at runtime
• Support eval
Wednesday, April 22, 15
14. “Dynamic Programming Language”
(according to Wikipedia)
And “Dynamically Typed” means:
“A programming language is dynamically typed when
the majority of type checking is performed at run-time
as opposed to compile time. In dynamic typing, values
have types but variables do not.”
Wednesday, April 22, 15
15. Some “Dynamic Languages”
(according to Wikipedia)
ActionScript
BeanShell*
Lisp
Groovy
JavaScript
Clojure VBScript
Matlab
Lua
Objective-C
Perl PHP
Python
Ruby
Smalltalk
Tcl
*I guess this means the bunny was right and wrong, at the very same time?
Wednesday, April 22, 15
16. Going Back in the Way-Back
Machine...
Wednesday, April 22, 15
17. Or: What I learned from Perl
(that I’ll admit to, and that served me well in
learning groovy, and javascript)
Back when I did C, I vaguely understood that among other
things a compiler lays out things so that generated machine
code knows the offset of fields within a structure.
This is what makes the generated code fast.
But it also makes it inflexible, because offsets are set in
stone at compile time.
Wednesday, April 22, 15
18. Statically Compiled Languages
(compile stuff into offsets that set like concrete)
struct student {
int id;
char *name;
float percent;
};
offset field
+0 id
+4 name
+8 percent
C Code: Offsets used in generated code:
compiler
Wednesday, April 22, 15
19. But Perl had a very different
approach to classes and objects...
They were just a Map.
This seemed crude, but combined with first class functions,
and the speed of edit/test versus edit/compile/test - I grew
to like Perl over C for many tasks.
Wednesday, April 22, 15
20. $student = {
id => 1,
name => “Jason”,
percent => 100.0
};
key value
id 1
name Jason
percent 100.0
Perl Code:
Runtime data structure:
interpreter
Dynamic Languages
(classes in data structures you can manipulate at run time)
Wednesday, April 22, 15
21. I grew to feel...
Perl was to clay,
as C,
was to concrete.
Wednesday, April 22, 15
22. Modern languages still carry
these distinctions
(though a little blurrier)
Java is still a high performance compiled statically typed
language, but with more run-time-stuff than C had (e.g. the
Reflection api).
And I still relate to the metaprogramming possibilities of
Groovy (Javascript, Ruby, Python, etc) by thinking “oh
yeah, it’s basically just a Map”.
Wednesday, April 22, 15
23. Groovy is Special
(because it’s a hybrid)
Groovy combines a static language (java) and a Dynamic
(smalltalk/ruby/perl like) language into one.
Groovy’s classes wrap a Map-like soft and chewy
Dynamic Language coating - i.e. “Metaclass” -
around a hard candy Static Language core - i.e. a normal
java Class.
(it’s like an M&M but inside out)
Wednesday, April 22, 15
24. Java Class Core
(fast but not so flexible)
Groovy Metaclass Coating
(think map)
Wednesday, April 22, 15
26. Exercise: Add “Factory New”
(or “Ruby Style New”)
Methods to Groovy
i.e. not:
def service = new ServiceClass();
but rather:
def service = ServiceInterface.new();
Wednesday, April 22, 15
27. Why?
•Some argue that the “new” keyword as practiced by
C, C++, and Java is flawed, e.g. see:
Java's new Considered Harmful
By Jonathan Amsterdam, April 01, 2002
http://drdobbs.com/184405016
•It’s an example of something that you can do pretty
easily in groovy that would be extremely difficult in
java.
•Esp. when you consider the result feels pretty much
like a native language feature to the programmer using
it.
Wednesday, April 22, 15
28. From “Java’s new Considered Harmful”
In a nutshell, the problem with
new is that it fails to encapsulate
object creation.
def service = new ServiceClass();
no encapsulation!
Wednesday, April 22, 15
29. So: let’s see if we can add “factory
new” methods to groovy classes.
Along with a smidge of what
Spring provides (e.g. configurable
singletons and prototypes)
Without using a container - just
groovy, from groovy scripts.
Wednesday, April 22, 15
30. Code samples for this talk
available at:
https://github.com/darrencruse/groovy-metaprogramming-samples
Wednesday, April 22, 15
Editor's Notes
Can anybody name this muppet character?
Dr. Bunsen Honeydew
Honestly the first thing is really me truly wanting to hear from you guys what you think about groovy adoption.
Are your company’s using it?
I did some snooping on the net and honestly was a little discouraged. That TIOBE language popularity index had it pretty far down.
Then my buddy interviewed at Enterprise Rent-A-Car and he tells me they’re really big on groovy.
So I’m confused.
Then the second thing relates partly to the fact that I’ve been working on a server-side javascript project recently and though I think it could as easily have used groovy, I’m not unhappy I’ve been thinking a lot about the similarity between these languages and feeling like my knowledge of groovy transfers to javascript and vice versa.
Then the last thing is using one of the code examples I’d done for the talk I gave here on metaprogramming a year ago when I’d not thought to bring my DVI to VGA adapter and so hadn’t gotten to show any code.
My real reason for putting this slide in is in the past year I’ve come to appreciate more how much I’m an early adopter.
And how I’ve tended to assume other people are like me when really I think this graph gets it right.
The innovators and early adopters are a minority.
I’d assumed that groovy would have exploded in popularity and have been much closer to java in popularity by now.
This is from a book I thought was really neat I’d recommend it if you feel you’re an early adopter and are interested in why cool stuff often doesn’t catch on faster.
The book is mainly talking about the people side of why things don’t catch on.
I included this diagram because I feel it’s pointing to something groovy does better than it’s competitors like Ruby.
After all groovy’s syntax is so close to java it makes learning it pretty easy. The “Ease of Adoption” seems pretty good to me.
Yet it’s not completely backward compatible you can’t *really* copy and paste java as groovy like you can beanshell.
And I’m not sure maybe for managers and executives who aren’t programmer types the “benefits of adoption” part still isn’t clear.
I loved Jack’s talk where he showed all those examples of how groovy cuts the lines of code compared to java.
Yet: At my company we use a lot of contractors so it seems using a well known language where there are lots of inexpensive programmers available seems a high priority.
And I wonder if managers would have warmed quicker to a name like “java++” or something than “groovy”.
But the curve is one the rise so hope I’m not sounding negative.
For me it’s just been a dose of reality and not being *unrealistic* about the rate of adopting new technologies.
I’ve started on a project incorporating server-side javascript (Rhino) on a Spring/Hibernate project with a team of java developers.
We’ve been discussing the benefits of doing so (and whether we should!).
I’ve noticed the similarities (and some differences) between Rhino and Groovy.
And this got me thinking back even to my Perl days and how I never thought much about they’re being a family of “Dynamic Languages”.
How do they differ from static languages, what are their strengths (why do I seem to like them?)
So this is the actual text from the comments on Graeme Rocher’s blog post from 2006.
In the comments Graeme and Bob Lee started having an argument over Groovy and BeanShell and the term “Dynamic Language”.
Graeme is project lead for Grails.
And Bob Lee is the creator of the Guice IOC container among other things.
Both are absolutely brilliant.
And I think there’s really interesting things said in the text.
Yet the discussion becomes a “Jane you ignorant slut!” type of argument when I read it I thought it was really funny.
It runs on for about 8 minutes I’m not sure we can afford the time if you guys feel it’s taking too long why don’t you give me a signal and if several of you do I’ll stop it early and just make sure to post the link on the google groups site.
These are the dynamic languages the wikipedia page on Dynamic Languages lists.
The ones available on the JVM I put in white (unless I’ve missed some?)
I noted that BeanShell is listed.
So when Bob Lee argued against the term “Dynamic Language” - at least he was wrong according to the guys who made this wikipedia page.
Yet Graeme was arguing Groovy and Ruby have features that make them a “Dynamic Language” and BeanShell didn’t.
But BeanShell’s listed here!
So they were both wrong and right at the same time?!
This thing about edit/test/edit/test just hit refresh kind of thing...
Versus edit/compile/test/edit/compile/test.
It’s funny even with the advances in these IDEs with auto-compile and (supposed) hot deployment and stuff.
I *still* feel slowed down too often having to restart my server or things (we use RAD and WebSphere).
Compared to something like groovy or javascript where you just edit and hit refresh on your browser.
100 times a day quick quick quick.
To this is the “usability” aspect of these languages that is hard for non-programmers to appreciate.
So I actually resisted Perl in the beginning and then ultimately wound up feeling it was really neat.
But then when I joined my current company 8 years ago I was really disappointed when I heard the development head say their goal was to replace all their Perl with java.
Not because I’m against java, but rather because I saw these languages as complementary.
I’d come to feel Perl brought something to the table. As does groovy.
I’m getting here not just at metaprogramming but at the idea that the quick edit/run/edit/run style of development makes for a more agile development style.
Where you can dive in and evolve the design as you go.
But maybe that’s not fair: I do sometimes love the refactoring tools in java.
But I think it’s also the brevity of the code, and the power that first class functions bring.
That makes me feel more productive.
Do we have some javascript people here?
I didn’t do much javascript for a long time but I got deeply into it a few years ago doing Dojo web 2.0 stuff.
And I remember how the Perl experience helped me relate to how prototypes work as runtime data structures.
And again - groovy is similar - if not in the details at least in the broad “model” for how to think about things.
I’ve started on a project incorporating server-side javascript (Rhino) on a Spring/Hibernate project with a team of java developers.
We’ve been discussing the benefits of doing so (and whether we should!).
I’ve noticed the similarities (and some differences) between Rhino and Groovy.
And this got me thinking back even to my Perl days and how I never thought much about they’re being a family of “Dynamic Languages”.
How do they differ from static languages, what are their strengths (why do I seem to like them?)
How do you mock ServiceClass?
The various factory patterns are all about working around this problem.
At NFJS I remember the first time I heard (from Dave Thomas I think) this idea that “Design Patterns are workarounds for language limitations”)