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

Groovy as a Dynamic Language

Jul 20, 2015

Download

Software

Darren Cruse
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Groovy as a Dynamic Language

Groovy as a Dynamic Language

Darren Cruse

(with just a smidge of metaprogramming)

Wednesday, April 22, 15

Page 2: Groovy as a Dynamic Language

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

Page 3: Groovy as a Dynamic Language

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

Page 4: Groovy as a Dynamic Language

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

Page 5: Groovy as a Dynamic Language

indeed.com

Wednesday, April 22, 15

Page 6: Groovy as a Dynamic Language

indeed.com

Wednesday, April 22, 15

Page 7: Groovy as a Dynamic Language

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

Page 8: Groovy as a Dynamic Language

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

Page 9: Groovy as a Dynamic Language

Dynamic Languages(are on my mind)

(Glenn Campbell song, 1973)

Wednesday, April 22, 15

Page 10: Groovy as a Dynamic Language

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

Page 11: Groovy as a Dynamic Language

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

Page 12: Groovy as a Dynamic Language

“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

Page 13: Groovy as a Dynamic Language

“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

Page 14: Groovy as a Dynamic Language

“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

Page 15: Groovy as a Dynamic Language

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

Page 16: Groovy as a Dynamic Language

Going Back in the Way-Back Machine...

Wednesday, April 22, 15

Page 17: Groovy as a Dynamic Language

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

Page 18: Groovy as a Dynamic Language

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

Page 19: Groovy as a Dynamic Language

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

Page 20: Groovy as a Dynamic Language

$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

Page 21: Groovy as a Dynamic Language

I grew to feel...

Perl was to clay, as C,

was to concrete.

Wednesday, April 22, 15

Page 22: Groovy as a Dynamic Language

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

Page 23: Groovy as a Dynamic Language

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

Page 24: Groovy as a Dynamic Language

Java Class Core(fast but not so flexible)

Groovy Metaclass Coating(think map)

Wednesday, April 22, 15

Page 25: Groovy as a Dynamic Language

FINALLY(some code!)

(Glenn Campbell song, 1973)

Wednesday, April 22, 15

Page 26: Groovy as a Dynamic Language

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

Page 27: Groovy as a Dynamic Language

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

Page 28: Groovy as a Dynamic Language

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

Page 29: Groovy as a Dynamic Language

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

Page 30: Groovy as a Dynamic Language

Code samples for this talk available at:

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

Wednesday, April 22, 15