1. Learning Groovy (in 3 hours)!
Adam L. Davis
The Solution Design Group, Inc.
Author of âLearning Groovyâ
(& Whatâs New in Java 8 & others)
14 years Java Dev.
github.com/adamldavis
/2017-gr8conf-learning-groovy
6. â Dynamic or Static
â (@CompileStatic @TypeChecked)
â As fast as Java (with static & indy)
â Meta-programming
â Optional semi-colons
â Optional parentheses
â Short-hand for Lists and Maps
â Automatic getters and setters
â A better switch
â Groovy GDKâŠ
Groovy 2.4 Features
7. â Closures
â Currying
â Method references
â Map/Filter/Reduce as collect, findAll, inject
â Internal iterating using each
â Operator Overloading (+ - * % / âŠ)
â methodMissing and propertyMissing
â AST Transformations
â Traits
âŠ
Groovy 2.4 Features (cont.)
8. Starting Out
Option 1: Using sdkman.io
â sdk install groovy 2.4.9
Option 2: Download from groovy-lang.org
â Alter your PATH
â Export PATH=$PATH:/usr/lib/groovy/bin
â Option 3: Mac â see http://groovy-lang.org/install.html
â Option Ï: Windows â see https://github.com/groovy/groovy-windows-installer
Then: $ groovyConsole
IntelliJ IDEA or NetBeans
9. Dynamic typing
â def keyword
â Parametersâ typing optional
â Possible to mock using a map
â def dog = [bark: { println âwoofâ }]
â Using @TypeChecked or @CompileStatic you
can make Groovy statically typed in some
classes
10. Groovy Strings
â ânormal stringâ
â âgroovy string can contain $variablesâ
â âcan also do expressions ${x + 1}â
â Use triple quote to start/end multi-line strings
âââ
This is a
Multi-line
String
âââ
11. Math, Groovy Truth, and Equals
â Numbers are BigDecimal by default not Double
â 3.14 is a BigDecimal
â 3.14d is a Double
â Groovy truth: null, ââ, [], 0 are false
â if (!thing) println âthing was nullâ
â if (!str) println âstr was emptyâ
â Groovy == means .equals
â For identity use a.is(b)
12. Property Access
â Everything is public
by default
â Every field has a
getter and setter by
default
â Gotchaâs
â Map access
â String.class->String
â Property access automatically
uses getters and setters
â foo.bar == foo.getBar()
â foo.bar = 2 == foo.setBar(2)
16. A better switch
â Switch can use types, lists, ranges, patternsâŠ
Switch (x) {
case Map: println âwas a mapâ; break
case [4,5,6]: println âwas 4, 5 or 6â; break
case 0..20: println âwas 0 to 20â; break
case ~/w+/: println â was a wordâ; break
case âhelloâ: println x; break
case BigDecimal: println âwas a BigDecimalâ
17. Groovy GDK
â Adds methods to everything! Adds its own classes...
â Collections: sort, findAll, collect, inject, each,âŠ
â IO: toFile(), text, bytes, withReader, URL.content
â Ranges: x..y, x..<y
â GetAt syntax for Strings and Lists:
â text[0..4] == text.substring(0,5)
â Utilities: ConfigSlurper, Expando, ObservableList/Map/Set
18. Safe dereference & Elvis operator
â Safe dereference ?.
â String name = person?.name
â Java: person == null ? null : person.getName()
â Elvis operator ?:
â String name = person?.name ?: âBobâ
â Java: if (name == null) name = âBobâ
19. Closures
â Closure: âa self-containing methodâ (like Lambda exp.)
â def say = { x -> println x }
â say(âhello gr8confâ)
â def say = { println it }
â def adder = { x, y -> x + y }
â Closures have several implicit variables:
â it - If the closure has one argument
â this - Refers to the enclosing class
â owner - The same as this unless it is enclosed in another closure.
â delegate - Usually the same as owner but you can change it (this
allows the methods of delegate to be in scope).
20. Closures Continued...
â When used as last parameter, closure can go
outside parentheses
â methodCalled(param1, param2) { closureHere() }
â methodWithOnlyClosure { closureHere() }
21. Regex Pattern Matching
â Regex = regular expressions
â Within forward slashes / is a regex
â You donât need to use double
â =~ for matching anywhere within a string
â if (text =~ /d+/) println âthere was a number in itâ
â ==~ for matching the whole string
â if (email ==~ /[w.]+@[w.]+/) println âitâs an emailâ
22. Meta-programming
â Every class and instance has a metaClass
â String.metaClass.upper =
{ delegate.toUpperCase() }
â âfooâ.upper() == âFOOâ
â Traits can be used as
mixins
â Maps can be cast to
actual types using as
[bark: {println âWoof!â}]
as Dog
28. Spock
Built on JUnit
Somewhat enhanced groovy
âtest names can be any stringâ
given: when: then: expect:
Built-in mocking
Table-syntax for provided test data
Pretty assert output
29. Thanks!
Adam L. Davis
âLearning Groovyâ
github.com/adamldavis
/2017-gr8conf-learning-groovy
adamldavis.com
@adamldavis
groocss.org
@groocss
How? Gedit + https://github.com/aeischeid/gedit-grails-bundle