groovy as a dynamic language

30
Groovy as a Dynamic Language Darren Cruse (with just a smidge of metaprogramming) Wednesday, April 22, 15

Upload: darren-cruse

Post on 20-Jul-2015

134 views

Category:

Software


0 download

TRANSCRIPT

Groovy as a Dynamic Language

Darren Cruse

(with just a smidge of metaprogramming)

Wednesday, April 22, 15

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

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

Survey 2At 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

indeed.com

Wednesday, April 22, 15

indeed.com

Wednesday, April 22, 15

image from: http://whiteafrican.com/tag/crossing-the-chasmWednesday, April 22, 15

Ease of Adoption(versus)

Benefits of Adoption

“Ease ofAdoption”

“Benefits of Adoption”

“Sweet spot ofinnovation”

Adapted From: The Myths of Innovation, by Scott Berkun

Wednesday, April 22, 15

Dynamic Languages(are on my mind)

(Glenn Campbell song, 1973)

Wednesday, April 22, 15

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

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

“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

“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

“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

Some “Dynamic Languages”(according to Wikipedia)

ActionScript

BeanShell*

LispGroovy

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

Going Back in the Way-Back Machine...

Wednesday, April 22, 15

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

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

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

$student = {  id => 1,  name => “Jason”, percent => 100.0};

key valueid 1

name Jasonpercent 100.0

Perl Code: Runtime data structure:

interpreter

Dynamic Languages(classes in data structures you can manipulate at run time)

Wednesday, April 22, 15

I grew to feel...

Perl was to clay, as C,

was to concrete.

Wednesday, April 22, 15

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

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

Java Class Core(fast but not so flexible)

Groovy Metaclass Coating(think map)

Wednesday, April 22, 15

FINALLY(some code!)

(Glenn Campbell song, 1973)

Wednesday, April 22, 15

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

Why?

•Some argue that the “new” keyword as practiced by C, C++, and Java is flawed, e.g. see:

Java's new Considered HarmfulBy Jonathan Amsterdam, April 01, 2002http://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

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

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

Code samples for this talk available at:

https://github.com/darrencruse/groovy-metaprogramming-samples

Wednesday, April 22, 15