What makes Groovy Groovy - Guillaume Laforge (Pivotal)

  • View
    938

  • Download
    1

Embed Size (px)

DESCRIPTION

Presented at JAX London 2013 Groovy is not a newcomer to the arena of alternative languages for the JVM. With over 1.7 million downloads a year, it's clearly ahead of the pack. But what makes it a great choice for your projects? - a flat learning curve for Java developers - its seamless Java integration where you can mix & mash Groovy & Java together - a malleable & concise syntax fit for Domain-Specific Languages - an interesting take on type safety - its rich ecosystem of projects: Grails, Gradle, GPars, Spock, Griffon, Geb...

Transcript

  • 1.What makes Groovy groovy? Guillaume Laforge @glaforge

2. Guillaume Laforge Groovy project lead at . !@glaforge http://glaforge.appspot.com 3. 1 t r a PThe Groovy vision 4. Simplify the life of (Java) developers 5. Groovy as a Java superset 6. Groovy as a Java supersetIts so easy to learn! 7. classMathSpecextendsSpecification{ def"maximumoftwonumbers"(){ expect: Math.max(a,b)==c !where: a|b||c 1|3||3 7|4||4 0|0||0 } } 8. As safe and fast as Java with static type checking & compilation 9. As safe and fast as Java with static type checking & compilation 10. newMarkupBuilder().html{ head{ title"TheScriptBowl" } !body{ div(class:"banner"){ p"Groovyrocks!" } } } 11. moveforwardat3.km/h Expressive, Concise, Readable 12. Speaking of conciseness... A full Spring app in the span of a tweet! @RestController classApp{ @RequestMapping("/") Stringhome(){"HelloWorld!"} } 13. 1.7million downloads per year 14. Great for scripting 15. Great for scriptingFit for DomainSpecific Languages 16. Great for scriptingFit for DomainSpecific LanguagesMost seamless integration & interoperability wih java! 17. Yup, were all using Groovy! 18. 2 t r a PCool Groovy gems 19. Most Java code is also valid Groovy code! 20. Most Java code is also valid Groovy code!Any Java developer is a Groovy developer! 21. Flat learning curve 22. Flat learning curveEasy to learn 23. Scripts versus Classes publicclassMain{ publicstaticvoidmain(String[]args){ System.out.println("Hello"); } }vs !18 24. Scripts versus Classes publicclassMain{ publicstaticvoidmain(String[]args){ System.out.println("Hello"); } }vs println"Hello" !18 25. Optional 26. SemicolonsOptional 27. Semicolons ParenthesesOptional 28. Semicolons ParenthesesOptional return keyword 29. Semicolons ParenthesesOptional return keywordpublic keyword 30. Semicolons ParenthesesOptional Typing! return keywordpublic keyword 31. Optional... publicclassGreeter{ privateStringowner; ! publicStringgetOwner(){ returnowner; } ! publicvoidsetOwner(Stringowner){ this.owner=owner; } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner; } } ! Greetergreeter=newGreeter(); greeter.setOwner("Guillaume"); ! System.out.println(greeter.greet("Marion")); !20 32. Optional...SemicolonspublicclassGreeter{ privateStringowner; ! publicStringgetOwner(){ returnowner; } ! publicvoidsetOwner(Stringowner){ this.owner=owner; } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner; } } ! Greetergreeter=newGreeter(); greeter.setOwner("Guillaume"); ! System.out.println(greeter.greet("Marion")); !20 33. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ returnowner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner("Guillaume") ! System.out.println(greeter.greet("Marion")) !21 34. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ returnowner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner("Guillaume") ! System.out.println(greeter.greet("Marion")) !22 35. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ returnowner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner("Guillaume") ! System.out.println(greeter.greet("Marion"))Parentheses!22 36. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ returnowner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion") !23 37. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ returnowner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ return"Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion")return keyword!23 38. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ owner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion") !24 39. Optional... publicclassGreeter{ privateStringowner ! publicStringgetOwner(){ owner } ! publicvoidsetOwner(Stringowner){ this.owner=owner } ! publicStringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion")public keyword!24 40. Optional... classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion") !25 41. Optional... classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! Greetergreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion")optional typing!25 42. Optional... classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion") !26 43. Optional... classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! System.out.printlngreeter.greet("Marion")handy println shortcut!26 44. Optional... classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! printlngreeter.greet("Marion") !27 45. Optional...verbose Java properties!classGreeter{ privateStringowner ! StringgetOwner(){ owner } ! voidsetOwner(Stringowner){ this.owner=owner } ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! printlngreeter.greet("Marion") !27 46. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! printlngreeter.greet("Marion") !28 47. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.setOwner"Guillaume" ! printlngreeter.greet("Marion")Property notation!28 48. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.owner"Guillaume" ! printlngreeter.greet("Marion") !29 49. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter() greeter.owner"Guillaume" ! printlngreeter.greet("Marion")Named argument constructor!29 50. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter(owner:"Guillaume") ! ! printlngreeter.greet("Marion") !30 51. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello"+name+",Iam"+owner } } ! defgreeter=newGreeter(owner:"Guillaume") ! ! printlngreeter.greet("Marion")Interpolated strings! (aka GStrings)!30 52. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello${name},Iam${owner}" } } ! defgreeter=newGreeter(owner:"Guillaume") ! ! printlngreeter.greet("Marion") !31 53. Optional... classGreeter{ Stringowner ! ! ! ! ! ! ! ! ! Stringgreet(Stringname){ "Hello${name},Iam${owner}" } } ! defgreeter=newGreeter(owner:"Guillaume") ! ! printlngreeter.greet("Marion")Lets reformat that mess of whitespace!!31 54. Optional... classGreeter{ Stringowner ! Stringgreet(Stringname){ "Hello${name},Iam${owner}" } } ! defgreeter=newGreeter(owner:"Guillaume") ! printlngreeter.greet("Marion")!32 55. Optional... publicclassGreeter{ privateStringowner; ! publicStringgetOwner(){ classGreeter{ returnowner; Stringowner } ! ! Stringgreet(Stringname){ publicvoidsetOwner(Stringowner){ "Hello${name},Iam${owner}" this.owner=owner; } } } ! ! publicStringgreet(Stringname){ defgreeter=newGreeter(owner:"Guillaume") return"Hello"+name+",Iam"+owner; ! } printlngreeter.greet("Marion") } ! Greetergreeter=newGreeter(); greeter.setOwner("Guillaume"); ! System.out.println(greeter.greet("Marion")); !32 56. Optional... classGreeter{ Stringowner ! Stringgreet(Stringname){ "Hello${name},Iam${owner}" } } ! defgreeter=newGreeter(owner:"Guillaume") ! printlngreeter.greet("Marion")!32 57. Native syntax constructs //closures defadder={a,b->a+b} !//lists deflist=[1,2,3,4,5] !//maps defmap=[a:1,b:2,c:3] !//regularexpressions defregex=~/.*foo.*/ !//ranges defrange128..255 !33 58. Closures the basicsdefadder={a,b->a+b} !assertadder(1,2)==3 assertadder('a','b')=='ab'!34 59. Closures the basics Closure parametersdefadder={a,b->a+b} !assertadder(1,2)==3 assertadder('a','b')=='ab'!34 60. Closures the basics Assign a function into a variableClosure parametersdefadder={a,b->a+b} !assertadder(1,2)==3 assertadder('a','b')=='ab'!34 61. Closures the basics Assign a function into a variableClosure parametersdefadder={a,b->a+b} !assertadder(1,2)==3 assertadder('a','b')=='ab' Short form of: adder.call(a, b) !34 62. Closures the basics Assign a function into a variableClosure pa