How Languages Influence Each Other: Reflections on 14 Years of Apache Groovy

How Languages Influence Each Other: Reflections on 14 Years of Apache Groovy

Languages have been influencing one another since the dawn of computer programming. There are families of languages: from Algol descendants with begin/end code blocks to those with curly braces such as C. Languages are not invented in a vacuum but are inspired by their predecessors. This session’s speaker, who has been working on Apache Groovy for the past 14 years, reflects on the influences that have driven the design of programming languages. In particular, Groovy’s base syntax was directly derived from Java’s but quickly developed its own flavor, adding closures, type inference, and operators from Ruby. Groovy also inspired other languages: C#, Swift, and JavaScript adopted Groovy’s null-safe navigation operator and the famous Elvis operator.

137d3908243acfc30e126615d59d4e6d?s=128

Guillaume Laforge

October 04, 2017
Tweet

Transcript

  1. 1.

    REFLECTION ON 14 YEARS OF APACHE GROOVY HOW LANGUAGES INFLUENCE

    EACH OTHER GUILLAUME LAFORGE Apache Groovy PMC Chair Developer Advocate @ Google Cloud
 @glaforge
  2. 2.
  3. 3.

    @glaforge INTRODUCTION APACHE GROOVY ▸ Groovy was born in 2003

    ▸ Created by James Strachan & Bob McWhirter ▸ I joined the project in 2003 — 14 years ago! ▸ Currently serving as Chair of the Project Management Committee ▸ Groovy moved to the Apache Software Foundation to become Apache Groovy ▸ after Pivotal stopped funding the project 3
  4. 4.

    @glaforge INTRODUCTION DOWNLOAD NUMBERS TO BE PROUD OF 4 Move

    to ASF 30M+ for 2/3 of ’17 23M in 2016 13M in 2015 4.6M in 2014 3.2M in 2013 1.7 in 2012
  5. 6.

    @glaforge C FAMILY APACHE GROOVY PART OF THE C-FAMILY ▸

    Like Java, C#, C++, JavaScript, D, Go, Swift, Dart, or even PHP :-)
 Apache Groovy is part of the C-family of languages ▸ Key syntax characteristics ▸ Curly braces delimit blocks ▸ No whitespace indentation ▸ Signatures of the form: type functionName(parameters) { … } ▸ Usual control structures: if/else, switch/case, for/while loops… 6
  6. 8.

    @glaforge C FAMILY JAVA’S CLOSE COUSIN — A VALID GROOVY

    PROGRAM TOO! 8 public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println(helloWorld.greet()); } }
  7. 9.

    @glaforge C FAMILY JAVA’S CLOSE COUSIN — MORE IDIOMATIC! 9

    class HelloWorld { String name def greet() { "Hello ${name}" } } def helloWorld = new HelloWorld(name: "Groovy") println helloWorld.greet()
  8. 12.

    @glaforge STRINGS STRINGS INSPIRED BY PYTHON 12 def firstname =

    'Guillaume' def address = ''' 1st Main Street CA 94123 San Francisco '''
  9. 13.

    @glaforge STRINGS STRINGS INSPIRED BY PYTHON 13 def firstname =

    "Guillaume" def address = """ 1st Main Street CA 94123 San Francisco """
  10. 14.

    @glaforge STRINGS STRING INTERPOLATION SIMILAR TO RUBY AND ANT 14

    def firstname = 'Guillaume' def lastname = 'Laforge' def name = "Mr ${firstname} $lastname"
  11. 15.

    @glaforge STRINGS LAZY INTERPOLATION 15 def a = 'a' def

    b = 'b' def s = "${a} ${-> b}" assert s == 'a b' a = 'x' b = 'y' assert s == 'a y'
  12. 18.

    @glaforge FUNCTIONAL ORIENTATION A FIRST APPROACH WITH RUBY’S AND SMALLTALK

    SYNTAX 18 // smalltalk [ :a | a * 2 ] // ruby {| a | a * 2 }
  13. 20.

    @glaforge FUNCTIONAL ORIENTATION PROBLEM WITH BINARY AND LOGICAL OR FOR

    DEFAULT PARAMETERS 20 def d = { a = 1 | 2 | a * 2 }
  14. 23.
  15. 24.

    @glaforge OBJECT ORIENTATION TRAITS… A LA SCALA THEATER 24 trait

    FlyingAbility { String fly() { "I'm flying!" } } trait SpeakingAbility { String speak() { "I'm speaking!" } } class Duck implements FlyingAbility, SpeakingAbility {} def d = new Duck() assert d.fly() == "I'm flying!" assert d.speak() == "I'm speaking!"
  16. 25.
  17. 26.

    @glaforge SMALLTALK INFLUENCE NAMED PARAMETERS 26 // smalltalk Rectangle red

    width: 100 height: 200 // groovy rectangle red, width: 100, height: 200 def rectangle(Map m, Color r) { println "$m.width:$m.height $r" }
  18. 27.

    @glaforge SMALLTALK INFLUENCE COLLECTION METHODS NAMES 27 // map //

    #(1, 2, 3, 4) collect: [ :it | :it * 2 ] assert (1..4).collect { it * 2 } == [2, 4, 6, 8] // foldLeft // #(1, 2, 3) inject: 0 into: [ :count :item | count + item ] assert [1, 2, 3].inject(0) { count, item -> count + item } == 6
  19. 29.

    @glaforge LISTS, MAPS, COLLECTIONS… NATIVE SYNTAX FOR DATA STRUCTURES 29

    // python lists def list = [1, 2, 3] // like python maps // but with square brackets def map = [a: 1, b: 2]
  20. 31.
  21. 33.

    @glaforge CLOSURE TRAILING CLOSURE 33 def unless(boolean b, Closure action)

    { if (!b) action() } unless(false, { println "xyz" }) unless(false) { println "xyz" }
  22. 35.

    @glaforge KOTLIN KOTLIN, A MIX OF JAVA, SCALA, AND… APACHE

    GROOVY ▸ Kotlin adopted ▸ the syntax of Groovy closures and its ‘it’ default parameter, ▸ the notation for trailing closures ▸ Groovy’s builder concept ▸ Kotlin’s data classes, delegate and lazy mechanisms, follow Groovy’s @Immutable, @Delegate and @Lazy transformations ▸ Method name convention based operator overloading 35
  23. 36.

    @glaforge KOTLIN KOTLIN, A MIX OF JAVA, SCALA, AND… APACHE

    GROOVY ▸ Kotlin adopted Groovy’s optional semicolons ▸ Groovy’s Elvis operator ▸ Groovy’s null-safe navigation operator ▸ but with a different approach with non/nullable types ▸ Groovy’s interpolated strings (dollar placeholders with & without curly braces) ▸ Groovy’s import aliasing 36
  24. 37.

    @glaforge KOTLIN GROOVY’S BUILDER 37 html { head { title

    ("a title") } body { h1 ("first header") p ("a paragraph") } } html { head { title {+"a title"} } body { h1 {+"first header"} p {+"a paragraph"} } }
  25. 44.
  26. 45.

    @glaforge INSPIRED OTHERS RANGES 45 // Ruby style initially //

    1..4 == [1, 2, 3, 4] and 1...4 == [1, 2, 3] // ..< is more visual assert 1..4 == [1, 2, 3, 4] assert 1..<4 == [1, 2, 3] // Swift adopted 1..<4
  27. 47.

    @glaforge INSPIRED OTHERS THE SPACESHIP OPERATOR 47 assert (1 <=>

    1) == 0 assert (1 <=> 2) == -1 assert (2 <=> 1) == 1 assert ('a' <=> 'z') == -1
  28. 51.

    THANKS Q & A GUILLAUME LAFORGE Apache Groovy PMC Chair

    Developer Advocate @ Google Cloud
 @glaforge