Andres Almiray | Canoo Engineering AG


Polyglot Programming
in the JVM	

 Or how I Learned to Stop Worrying and Love
                   the JVM
About the speaker	

    Java developer since the beginning
    True believer in open source
    Groovy committer since 2007
    Project lead of the Griffon framework
    Currently working for
Some facts about Java	

    Previous name was Oak. Bonus points for
     knowing its real name before that
    Made its public appearance in 1995
    C/C++ were king at the time
    Networking, multithreading were baked
     right into the language
    Developers came for the applets and stayed
     for the components (JEE and all that jazz)
However...	

    It is already in its teens
    It has not seen a major feature upgrade
     since JDK5 was released back in 2004 ->
     generics (and we do know how that turned
     out to be, don’t we?)
    JDK7 has been delayed again (late 2010).
     Some features might or might not make
     the cut (the closures debacle)
More so...	

    It is rather verbose when compared to how
     other languages do the same task
    Its threading features are no longer
     enough. Concurrent programs desperately
     cry for immutability these days
Truth or myth?	

    Is Java oftenly referred as overengineered?
    Can you build a Java based web application
     (for arguments sake a basic Twitter clone)
     in less than a day‘s work WITHOUT an IDE?
    Did James Gosling ever say he was
     threatened with bodily harm should
     operator overloading find its way into Java?
The JVM is a great place to
work however Java makes it
   painful sometimes...
What can we do about it?!
Disclaimer
                       	

(this is not a bash-the-other-languages talk)
Reduced Verbosity
Standard Beans	

public class Bean {
  private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
}
Standard Beans	

public class Bean {
  private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
}
Standard Beans	



class Bean {
  String name
}
Standard Beans	



class Bean(var name:String)


class Bean {
  @scala.reflect.BeanProperty
  var name: String
}
Standard Beans	



(defstruct Bean :name)
Closures (or Functions)	

public interface Adder {
    int add(int a, int b);
}

public class MyAdder implements Adder {
  public int add(int a, int b) {
    return a + b;
    }
}
Closures (or Functions)	



def adder = { a , b -> a + b }
Closures (or Functions)	



def adder(a: Int, b: Int) = a + b
Closures (or Functions)	



(defn adder [a b] (+ a b))
Enhanced switch	

char letterGrade(int grade) {
  if(grade >= 0 && grade <= 60) return ‘F‘;
  if(grade > 60 && grade <= 70) return ‘D‘;
  if(grade > 70 && grade <= 80) return ‘C‘;
  if(grade > 80 && grade <= 90) return ‘B‘;
  if(grade > 90 && grade <= 100) return ‘A‘;
  throw new IllegalArgumentException(‘invalid
grade ‘+grade);
}
Enhanced Switch	

def letterGrade(grade) {
  switch(grade) {
     case 90..100: return ‘A‘
     case 80..<90: return ‘B‘
     case 70..<80: return ‘C‘
     case 60..<70: return ‘D‘
     case 0..<60: return ‘F‘
     case ~"[ABCDFabcdf]":
          return grade.toUpperCase()
  }
  throw new IllegalArgumentException
(‘invalid grade ‘+grade)
}
Enhanced Switch	



val VALID_GRADES = Set("A", "B", "C", "D", "F")
def letterGrade(value: Any):String = value match
{
  case x:Int if (90 to 100).contains(x) => "A"
  case x:Int if (80 to 90).contains(x) => "B"
  case x:Int if (70 to 80).contains(x) => "C"
  case x:Int if (60 to 70).contains(x) => "D"
  case x:Int if (0 to 60).contains(x) => "F"
  case x:String if VALID_GRADES(x.toUpperCase) =>
x.toUpperCase()
}
Enhanced Switch	


(defn letter-grade [grade]
  (cond
   (in grade 90 100) "A"
   (in grade 80 90) "B"
   (in grade 70 80) "C"
   (in grade 60 70) "D"
   (in grade 0 60) "F"
   (re-find #"[ABCDFabcdf]" grade)
(.toUpperCase grade)))
Java Interoperability
All of these are true	

    Java can call Groovy, Scala and Clojure
     classes as if they were Java classes
    Groovy, Scala and Clojure can call Java code
     without breaking a sweat


    In other words, interoperability with Java is
     a given. No need for complicated bridges
     between languages (i.e. JSR 223)
Ok, so...	

What else can these
  languages do?
All of them 	

    Native syntax for collection classes
    Everything is an object
    Closures!
    Regular expressions as first class citizens
    Operator overloading
Groovy	

    Metaprogramming (HOT!) both at buildtime
     and runtime
    Builders
    Healthy ecosystem: Grails, Griffon, Gant,
     Gradle, Spock, Gaelyk, Gpars, CodeNarc,
     etc...


    Not an exhaustive list of features!
Scala	

    Richer type system
    Type inference
    Pattern matching (case classes)
    Actor model




    Not an exhaustive list of features!
Clojure	

    Data as code and viceversa
    Immutable structures
    STM




    Not an exhaustive list of features!
Demo
Other places where you‘ll
find Polyglot Programming
Web app development	

    XML
    SQL
    JavaScript
    JSON
    CSS
    Flash/Flex/ActionScript
Next-Gen datastores (NoSQL)	

    FleetDB -> Clojure


    FlockDB -> Scala


    CouchDB, Riak -> Erlang



    btw watch out for Polyglot Persistence ;-)
Build systems	

    Gradle, Gant -> Groovy


    Rake -> Ruby/JRuby


    Maven3 -> XML, Groovy, Ruby
Parting thoughts	

    Java (the language) may have reached its
     maturity feature wise
    Other JVM languages evolved faster
    Polyglot Programming is not a new concept
    Download and play with each of the
     demoed languages, maybe one of them
     strikes your fancy
Resources

Polyglot Programming @ Jax.de 2010

  • 1.
    Andres Almiray |Canoo Engineering AG Polyglot Programming in the JVM Or how I Learned to Stop Worrying and Love the JVM
  • 2.
    About the speaker   Java developer since the beginning   True believer in open source   Groovy committer since 2007   Project lead of the Griffon framework   Currently working for
  • 3.
    Some facts aboutJava   Previous name was Oak. Bonus points for knowing its real name before that   Made its public appearance in 1995   C/C++ were king at the time   Networking, multithreading were baked right into the language   Developers came for the applets and stayed for the components (JEE and all that jazz)
  • 4.
    However...   It is already in its teens   It has not seen a major feature upgrade since JDK5 was released back in 2004 -> generics (and we do know how that turned out to be, don’t we?)   JDK7 has been delayed again (late 2010). Some features might or might not make the cut (the closures debacle)
  • 5.
    More so...   It is rather verbose when compared to how other languages do the same task   Its threading features are no longer enough. Concurrent programs desperately cry for immutability these days
  • 6.
    Truth or myth?   Is Java oftenly referred as overengineered?   Can you build a Java based web application (for arguments sake a basic Twitter clone) in less than a day‘s work WITHOUT an IDE?   Did James Gosling ever say he was threatened with bodily harm should operator overloading find its way into Java?
  • 7.
    The JVM isa great place to work however Java makes it painful sometimes...
  • 8.
    What can wedo about it?!
  • 10.
    Disclaimer (this is not a bash-the-other-languages talk)
  • 11.
  • 12.
    Standard Beans public classBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
  • 13.
    Standard Beans public classBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
  • 14.
  • 15.
    Standard Beans class Bean(varname:String) class Bean { @scala.reflect.BeanProperty var name: String }
  • 16.
  • 17.
    Closures (or Functions) publicinterface Adder { int add(int a, int b); } public class MyAdder implements Adder { public int add(int a, int b) { return a + b; } }
  • 18.
    Closures (or Functions) defadder = { a , b -> a + b }
  • 19.
    Closures (or Functions) defadder(a: Int, b: Int) = a + b
  • 20.
    Closures (or Functions) (defnadder [a b] (+ a b))
  • 21.
    Enhanced switch char letterGrade(intgrade) { if(grade >= 0 && grade <= 60) return ‘F‘; if(grade > 60 && grade <= 70) return ‘D‘; if(grade > 70 && grade <= 80) return ‘C‘; if(grade > 80 && grade <= 90) return ‘B‘; if(grade > 90 && grade <= 100) return ‘A‘; throw new IllegalArgumentException(‘invalid grade ‘+grade); }
  • 22.
    Enhanced Switch def letterGrade(grade){ switch(grade) { case 90..100: return ‘A‘ case 80..<90: return ‘B‘ case 70..<80: return ‘C‘ case 60..<70: return ‘D‘ case 0..<60: return ‘F‘ case ~"[ABCDFabcdf]": return grade.toUpperCase() } throw new IllegalArgumentException (‘invalid grade ‘+grade) }
  • 23.
    Enhanced Switch val VALID_GRADES= Set("A", "B", "C", "D", "F") def letterGrade(value: Any):String = value match { case x:Int if (90 to 100).contains(x) => "A" case x:Int if (80 to 90).contains(x) => "B" case x:Int if (70 to 80).contains(x) => "C" case x:Int if (60 to 70).contains(x) => "D" case x:Int if (0 to 60).contains(x) => "F" case x:String if VALID_GRADES(x.toUpperCase) => x.toUpperCase() }
  • 24.
    Enhanced Switch (defn letter-grade[grade] (cond (in grade 90 100) "A" (in grade 80 90) "B" (in grade 70 80) "C" (in grade 60 70) "D" (in grade 0 60) "F" (re-find #"[ABCDFabcdf]" grade) (.toUpperCase grade)))
  • 25.
  • 26.
    All of theseare true   Java can call Groovy, Scala and Clojure classes as if they were Java classes   Groovy, Scala and Clojure can call Java code without breaking a sweat   In other words, interoperability with Java is a given. No need for complicated bridges between languages (i.e. JSR 223)
  • 27.
    Ok, so... What elsecan these languages do?
  • 28.
    All of them   Native syntax for collection classes   Everything is an object   Closures!   Regular expressions as first class citizens   Operator overloading
  • 29.
    Groovy   Metaprogramming (HOT!) both at buildtime and runtime   Builders   Healthy ecosystem: Grails, Griffon, Gant, Gradle, Spock, Gaelyk, Gpars, CodeNarc, etc...
   Not an exhaustive list of features!
  • 30.
    Scala   Richer type system   Type inference   Pattern matching (case classes)   Actor model
   Not an exhaustive list of features!
  • 31.
    Clojure   Data as code and viceversa   Immutable structures   STM
   Not an exhaustive list of features!
  • 32.
  • 33.
    Other places whereyou‘ll find Polyglot Programming
  • 34.
    Web app development   XML   SQL   JavaScript   JSON   CSS   Flash/Flex/ActionScript
  • 35.
    Next-Gen datastores (NoSQL)   FleetDB -> Clojure
   FlockDB -> Scala
   CouchDB, Riak -> Erlang   btw watch out for Polyglot Persistence ;-)
  • 36.
    Build systems   Gradle, Gant -> Groovy
   Rake -> Ruby/JRuby
   Maven3 -> XML, Groovy, Ruby
  • 37.
    Parting thoughts   Java (the language) may have reached its maturity feature wise   Other JVM languages evolved faster   Polyglot Programming is not a new concept   Download and play with each of the demoed languages, maybe one of them strikes your fancy
  • 38.