polyglot programming in the jvm
TRANSCRIPT
![Page 1: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/1.jpg)
Polyglot Programming in the JVM
Or how I Learned to Stop Worrying and Love the JVM
Andres Almiray | Canoo Engineering AG
![Page 2: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/2.jpg)
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
![Page 3: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/3.jpg)
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)
![Page 4: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/4.jpg)
However...
It‘s 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 Some features might or might not make the cut
(the closures debacle)
late 2010late 2010early 2011?early 2011?
![Page 5: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/5.jpg)
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
![Page 6: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/6.jpg)
Truth or myth?
Is Java oftenly referred as overengineered?
![Page 7: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/7.jpg)
Truth or myth?
Can you build a Java based web application (for argument sake a basic Twitter clone) in
less than a day‘s work WITHOUT an IDE?
![Page 8: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/8.jpg)
Truth or myth?
Did James Gosling ever say he was threatened with bodily harm should operator
overloading find its way into Java?
![Page 9: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/9.jpg)
The JVM is a great place to work however Java makes it painful sometimes...
![Page 10: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/10.jpg)
What can we do about it?!
![Page 11: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/11.jpg)
![Page 12: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/12.jpg)
Disclaimer
(this is not a bash-the-other-languages talk)
![Page 13: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/13.jpg)
Reduced Verbosity
![Page 14: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/14.jpg)
Standard Beanspublic class Bean { private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
![Page 15: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/15.jpg)
Standard Beanspublic class Bean { private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
![Page 16: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/16.jpg)
Standard Beanspublic class Bean { private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
![Page 17: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/17.jpg)
Standard Beans
class Bean {
String name
}
![Page 18: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/18.jpg)
Standard Beans
class Bean(var name:String)
class Bean { @scala.reflect.BeanProperty var name: String}
![Page 19: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/19.jpg)
Standard Beans
(defstruct Bean :name)
![Page 20: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/20.jpg)
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; }
}
![Page 21: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/21.jpg)
Closures (or Functions)
// JDK7 Lambdas proposal
#(int a, int b)(a + b)
#(int a, int b) { return a + b; }
![Page 22: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/22.jpg)
Closures (or Functions)
def adder = { a , b -> a + b }
![Page 23: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/23.jpg)
Closures (or Functions)
val adder = (a:Int, b:Int) => a + b
![Page 24: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/24.jpg)
Closures (or Functions)
(defn adder [a b] (+ a b))
![Page 25: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/25.jpg)
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);
}
![Page 26: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/26.jpg)
Enhanced Switchdef 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)}
![Page 27: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/27.jpg)
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()
}
![Page 28: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/28.jpg)
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)))
![Page 29: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/29.jpg)
Java Interoperability
![Page 30: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/30.jpg)
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)
![Page 31: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/31.jpg)
Ok, so... What else can these languages do?
![Page 32: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/32.jpg)
All of them
Native syntax for collection classes Everything is an object Closures! Regular expressions as first class citizens Operator overloading
![Page 33: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/33.jpg)
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!
![Page 34: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/34.jpg)
Scala
Richer type system Type inference Pattern matching (case classes) Actor model Traits
Not an exhaustive list of features!
![Page 35: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/35.jpg)
Clojure
Data as code and viceversa Immutable structures STM
Not an exhaustive list of features!
![Page 36: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/36.jpg)
Demo
![Page 37: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/37.jpg)
Other places where you‘ll find Polyglot Programming
![Page 38: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/38.jpg)
Web app development
XML SQL JavaScript JSON CSS Flash/Flex/ActionScript
![Page 39: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/39.jpg)
Next-Gen datastores (NoSQL)
FleetDB -> Clojure
FlockDB -> Scala
CouchDB, Riak -> Erlang
By the way, watch out for Polyglot Persistence ;-)
![Page 40: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/40.jpg)
Build systems
Gradle, Gant -> Groovy
Rake -> Ruby/JRuby
Maven3 -> XML, Groovy, Ruby
![Page 41: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/41.jpg)
Parting thoughts
Java (the language) may have reached its maturity feature wise
Other JVM languages have 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
![Page 42: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/42.jpg)
Resources
![Page 43: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/43.jpg)
Q & A
![Page 44: Polyglot Programming in the JVM](https://reader034.vdocuments.us/reader034/viewer/2022050720/554e919db4c905fc368b4c89/html5/thumbnails/44.jpg)
Thank you!