| Copyright © 2011, Oracle and/or it’s affiliates. All rights Insert Information Protection Policy Classification from Sli...
The Not Java Thats Not Scala:       Alternatives for EE Development       Justin Lee, Oracle            | Copyright © 2011...
Introduction           Who am I?           Using Java since 1996           GlassFish and Grizzly team member          ...
Introduction       Who am I not?       A Scala hater       A language geek       Compiler jockeyWednesday, October 5, 11
Why Not Java?          Java is 15+ years old now             Long time since the last release                Pace of ch...
Why Not Scala?          Lots of advanced features that are not readily accessible           to “Average Joes”          “...
So Whats Missing?              Closures              Syntax Cleanups              Succinctness               Typing/In...
So Whats Missing?          Functional programming          Mutability          Null Handling          Extendability   ...
So … what would you suggest?          Suggest is a strong word but...          Fantom          Gosu          New to me...
Fantom (Formerly known as Fan)          http://fantom.org          Familiar Syntax          Functional          Static...
Fantom - Portability          Fcode          JVM (of course)          .Net (partial)          JavaScriptWednesday, Oct...
Fantom          Literals                maps: [1:"one", 2:"two"] , empty [:]                lists: [0, 1, 2], empty [,]...
Fantom      public class Person {          private String name;          private int age;          public Person(String s,...
Fantom      class Person {          Str name          new make(String s, Int x) { name = s; age = x; }          Int age { ...
Fantom      Inheritance and Mixins:      class Foo {                                               class Bob : Foo, Bar { ...
Fantom      Closures      ["red", "yellow", "orange"].each |Str color| { echo(color) }      10.times |Int i| { echo(i) }  ...
Fantom      Dynamic typing      obj->foo             // obj.trap("foo", [,])      Nullable Types      Str // never stores ...
Fantom      Immutability      const class Point{          new make(Int x, Int y) { this.x = x; this.y = y }          const...
Fantom      Concurrency      // spawn actor which aynchronously increments an Int msg      actor := Actor(group) |Int msg-...
Fantom      Standard Libraries      // get file over HTTP      WebClient(`http://fantom.org`).getStr      // get tomorrows...
Fantom      Modularity and Meta-Programming      // find pods available for installation in project "Cool Proj"      repo ...
Fantom      Modularity and Meta-Programming      // find all types installed in system which subclass Widget      Pod.list...
Fantom      Modularity and Meta-Programming      // find pods available for installation in project "Cool Proj"      repo ...
Fantom      Modularity and Meta-Programming      // find pods available for installation in project "Cool Proj"      repo ...
Fantom       DSLs          embed other languages in your fantom code          similar to CDATA sections in XML         ...
Fantom      class RegexDslPlugin : DslPlugin {        new make(Compiler c) : super(c) {}        override Expr compile(DslE...
Fantom - Portability      @Js      class Demo {          Void main() {              // dumps to your browsers JS console  ...
Fantom - Tales  @Js class ClickClickJs {                 class Routes{    Void main() {                            Route[]...
Fantom - Draft      const class MyMod : DraftMod {        new make() {            pubDir = `...`.toFile            router ...
Fantom - Draft      Void index() {           res.headers["Content-Type"] = "text/plain"           res.out.printLine("Hi th...
Fantom - Resources          http://fantom.org #fantom on irc.freenode.net          http://www.talesframework.org/       ...
Gosu          http://gosu-lang.org          Again, familiar syntax          Statically typed          Enhancements    ...
Gosu          == Object equality          === Instance equality          >, <, etc. works with java.lang.Comparable    ...
Gosu - Properties!      public class MyJavaPersonClass {       String _name;                                            va...
Gosu    uses java.util.List    class SampleClass {     var _names : List<String> // a private class variable, which is a l...
Gosu    var c = new SampleClass({"joe", "john", "jack"})    c.printNames("* ")    * joe    * john    * jack    print( c.Na...
Gosu    uses java.util.List    class SampleClass {     var _names : List<String>        construct( names : List<String> ) ...
Gosu    uses java.util.List    class SampleClass {     var _names : List<String> as Names        construct( names : List<S...
Gosu    uses java.util.List    class SampleClass {     var _names : List<String> as readonly Names        construct( names...
Gosu    uses java.util.List    class SampleClass {     var _names : List<String> as readonly Names        construct( names...
Gosu    class MyRunnable implements Runnable {     delegate _runnable represents Runnable        construct() {          _r...
Gosu    class MyRunnable implements Runnable {     delegate _runnable represents Runnable        construct() {          _r...
Gosu - Donkey Patching    Extensions    enhancement MyStringEnhancement : String {      function printMe() {        print(...
Gosu      Enhancements         String: rightPad(), leftPad(), center(), toDate(), toBoolean()         File: read(), writ...
Gosu    Now for the fun part.Wednesday, October 5, 11
Gosu    Now for the fun part.    XML marshalling!Wednesday, October 5, 11
Gosu    Now for the fun part.    XML marshalling!    JAXB is “great” and all but it can be … painful.Wednesday, October 5,...
Gosu    Now for the fun part.    XML marshalling!    JAXB is “great” and all but it can be … painful.    Gosu has great al...
Gosu    Now for the fun part.    XML marshalling!    JAXB is “great” and all but it can be … painful.    Gosu has great al...
Gosu    <?xml version="1.0"?>     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">      <xs:element name="Person"> ...
Gosu    var bob = new myapp.example.Person()    bob.Age = 32    bob.Firstname = "Bob"    bob.Lastname = "Loblaw"    bob.pr...
Gosu    var bob = new myapp.example.Person()    bob.Age = 32    bob.Firstname = "Bob"    bob.Lastname = "Loblaw"    bob.pr...
Gosu    var bob = new myapp.example.Person()    bob.Age = 32    bob.Firstname = "Bob"    bob.Lastname = "Loblaw"    bob.pr...
Gosu    Feature Literals       var sub = String#substring(int) // class reference       print( sub.invoke( "Now is the tim...
Gosu    Feature Literals       var sub = String#substring(int) // class reference       print( sub.invoke( "Now is the tim...
Gosu    Feature Literals       var sub = String#substring(int) // class reference       print( sub.invoke( "Now is the tim...
Gosu - Ronin    <%@ extends ronin.RoninTemplate %>    <%@ params(name : String) %>    <html>      <body>         Hello ${n...
Gosu - Ronin     package controller      uses ronin.*      class Main extends RoninController {        function hello(name...
Gosu         http://www.gosu-lang.org         Web framework http://ronin-web.org/         ORM: http://ronin-web.org/Tos...
Justin Lee, Oracle       http://antwerkz.com       @evanchooly       http://github.com/evanchooly       http://www.basemen...
Upcoming SlideShare
Loading in …5
×

The Not Java That's Not Scala

3,544 views
3,406 views

Published on

My JavaOne talk covering Fantom and Gosu as non-Scala alternatives for use on the JVM.

Published in: Technology, News & Politics

The Not Java That's Not Scala

  1. 1. | Copyright © 2011, Oracle and/or it’s affiliates. All rights Insert Information Protection Policy Classification from Slide 8 reserved. |Wednesday, October 5, 11
  2. 2. The Not Java Thats Not Scala: Alternatives for EE Development Justin Lee, Oracle | Copyright © 2011, Oracle and/or it’s affiliates. All rights Insert Information Protection Policy Classification from Slide 8 reserved. |Wednesday, October 5, 11
  3. 3. Introduction  Who am I?  Using Java since 1996  GlassFish and Grizzly team member  websockets  Basement Coder  Used various languages over the years  python, c, c++, scala, ...Wednesday, October 5, 11
  4. 4. Introduction  Who am I not?  A Scala hater  A language geek  Compiler jockeyWednesday, October 5, 11
  5. 5. Why Not Java?  Java is 15+ years old now  Long time since the last release  Pace of change is geological  Verbosity  The shine has worn off  Its not cool!  Its “enterprisey”  it doesnt have my favorite feature!Wednesday, October 5, 11
  6. 6. Why Not Scala?  Lots of advanced features that are not readily accessible to “Average Joes”  “Academics are driving the bus” - James Gosling  Complexity - real and perceived  new fangled features  esoteric features  rampant feature abuse: trait **->**->** {implicit def **->**- >**[A, F[_, _], B](a: F[A, B]): *->*->*[A, F, B] = new *->*- >*[A, F, B] {val value = a}}Wednesday, October 5, 11
  7. 7. So Whats Missing?  Closures  Syntax Cleanups  Succinctness  Typing/Inference  Generics  ConcurrencyWednesday, October 5, 11
  8. 8. So Whats Missing?  Functional programming  Mutability  Null Handling  Extendability  Extension methods  Mixins  ModularityWednesday, October 5, 11
  9. 9. So … what would you suggest?  Suggest is a strong word but...  Fantom  Gosu  New to me, too, to one degree or anotherWednesday, October 5, 11
  10. 10. Fantom (Formerly known as Fan)  http://fantom.org  Familiar Syntax  Functional  Static and Dynamic Typing  “Modern” concurrency facilities  Portable  compile to JavaScript and SWT bothWednesday, October 5, 11
  11. 11. Fantom - Portability  Fcode  JVM (of course)  .Net (partial)  JavaScriptWednesday, October 5, 11
  12. 12. Fantom  Literals  maps: [1:"one", 2:"two"] , empty [:]  lists: [0, 1, 2], empty [,]  URIs: `/dir/file.txt`  Types: Int#  Slots: Int#plusWednesday, October 5, 11
  13. 13. Fantom public class Person { private String name; private int age; public Person(String s, int x) { name = s; age = x; } public String getName() { return name; } public void setName(String x) { name = x; } public int getAge() { return age; } public void setAge(int x) { checkAge(age); age = x; } int yearsToRetirement(int retire) { return (retire == -1 ? 65 : retire) - age } }Wednesday, October 5, 11
  14. 14. Fantom class Person { Str name new make(String s, Int x) { name = s; age = x; } Int age { set { checkAge(it); &age = it } Int yearsToRetirement(Int retire := 65) { return retire - age } }Wednesday, October 5, 11
  15. 15. Fantom Inheritance and Mixins: class Foo { class Bob : Foo, Bar { virtual Void bar() { echo("foo") } override baz := 10 } override Void bar() { echo("bob") } mixin Bar { } abstract Int baz Void yep(Int x) { baz = x } }Wednesday, October 5, 11
  16. 16. Fantom Closures ["red", "yellow", "orange"].each |Str color| { echo(color) } 10.times |Int i| { echo(i) } 10.times |i| { echo(i) } 10.times { echo(it) } files = files.sort |a, b| { a.modified <=> b.modified } Functions add := |Int a, Int b->Int| { return a + b } nine := add(4, 5)Wednesday, October 5, 11
  17. 17. Fantom Dynamic typing obj->foo // obj.trap("foo", [,]) Nullable Types Str // never stores null Str? // might store nullWednesday, October 5, 11
  18. 18. Fantom Immutability const class Point{ new make(Int x, Int y) { this.x = x; this.y = y } const Int x const Int y } const static Point origin := Point(0, 0) stooges := [“Moe”,”Larry”,”Curly”].toImmutableWednesday, October 5, 11
  19. 19. Fantom Concurrency // spawn actor which aynchronously increments an Int msg actor := Actor(group) |Int msg->Int| { msg + 1 } // send some messages to the actor and block for result 5.times echo(actor.send(it).get)Wednesday, October 5, 11
  20. 20. Fantom Standard Libraries // get file over HTTP WebClient(`http://fantom.org`).getStr // get tomorrows date Date.today + 1day // date literals // compute HTTP Basic auth string res.headers["Authorization"] = "Basic " + "$user:$pass".toBuf.toBase64Wednesday, October 5, 11
  21. 21. Fantom Modularity and Meta-Programming // find pods available for installation in project "Cool Proj" repo := Repo.makeForUri(`http://my-fantom-repo/`) pods := repo.query(Str<|"* proj.name=="Cool Proj"|>) pods.each |p| { echo("$p.name $p.version $p.depends") }Wednesday, October 5, 11
  22. 22. Fantom Modularity and Meta-Programming // find all types installed in system which subclass Widget Pod.list.each |pod| { pod.types.each |type| { if (type.fits(Widget#)) echo(type.qname) } }Wednesday, October 5, 11
  23. 23. Fantom Modularity and Meta-Programming // find pods available for installation in project "Cool Proj" repo := Repo.makeForUri(`http://my-fantom-repo/`) pods := repo.query(Str<|"* proj.name=="Cool Proj"|>) pods.each |p| { echo("$p.name $p.version $p.depends") }Wednesday, October 5, 11
  24. 24. Fantom Modularity and Meta-Programming // find pods available for installation in project "Cool Proj" repo := Repo.makeForUri(`http://my-fantom-repo/`) pods := repo.query(Str<|"* proj.name=="Cool Proj"|>) pods.each |p| { echo("$p.name $p.version $p.depends") }Wednesday, October 5, 11
  25. 25. Fantom DSLs  embed other languages in your fantom code  similar to CDATA sections in XML  AnchorType <|...|>  echo(Str <|now is the time for all good men to come to the aid of their country.|>)  Regex <|^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$|>Wednesday, October 5, 11
  26. 26. Fantom class RegexDslPlugin : DslPlugin { new make(Compiler c) : super(c) {} override Expr compile(DslExpr dsl) { regexType := ns.resolveType("sys::Regex") fromStr := regexType.method("fromStr") args := [Expr.makeForLiteral(dsl.loc, ns, dsl.src)] return CallExpr.makeWithMethod(dsl.loc, null, fromStr, args) } } // register in indexed props in build script index = ["compiler.dsl.sys::Regex": "compiler::RegexDslPlugin"]Wednesday, October 5, 11
  27. 27. Fantom - Portability @Js class Demo { Void main() { // dumps to your browsers JS console 10.times |x| { echo("x: $x") } }Wednesday, October 5, 11
  28. 28. Fantom - Tales @Js class ClickClickJs { class Routes{ Void main() { Route[] routes := Route[ Route{map="/clickclick"; Jq.ready { to="ClickClick";} Jq("#click").click|cur, event| { ] Win.cur.alert("Click Click") } event.preventDefault } } } }Wednesday, October 5, 11
  29. 29. Fantom - Draft const class MyMod : DraftMod { new make() { pubDir = `...`.toFile router = Router { routes = [ Route("/", "GET", #index), Route("/echo/{name}/{age}", "GET", #echo) ] } }Wednesday, October 5, 11
  30. 30. Fantom - Draft Void index() { res.headers["Content-Type"] = "text/plain" res.out.printLine("Hi there!") } Void echo(Str:Str args) { name := args["name"] age := args["age"].toInt res.headers["Content-Type"] = "text/plain" res.out.printLine("Hi $name, you are $age years old!") }Wednesday, October 5, 11
  31. 31. Fantom - Resources  http://fantom.org #fantom on irc.freenode.net  http://www.talesframework.org/  Draft Mini Web Framework https://bitbucket.org/ afrankvt/draft/wiki/Home  http://spectreframework.org/  Twitter  @afrankvt  @briansfrankWednesday, October 5, 11
  32. 32. Gosu  http://gosu-lang.org  Again, familiar syntax  Statically typed  Enhancements  Closures  Simplified generics  Open Type SystemWednesday, October 5, 11
  33. 33. Gosu  == Object equality  === Instance equality  >, <, etc. works with java.lang.Comparable  Standard logical operators  Null Safety print( x?.length ) // prints "null"  var zeroToTenInclusive = 0..10Wednesday, October 5, 11
  34. 34. Gosu - Properties! public class MyJavaPersonClass { String _name; var p = new MyJavaPersonClass() public String getName() { p.Name = "Joe" return _name; print( "The name of this person is ${p.Name}") } public void setName(String s) { _name = s; } }Wednesday, October 5, 11
  35. 35. Gosu uses java.util.List class SampleClass { var _names : List<String> // a private class variable, which is a list of Strings construct( names : List<String> ) { _names = names } function printNames( prefix : String ) { for( n in _names ) { print( prefix + n ) } } property get Names() : List<String> { return _names } }Wednesday, October 5, 11
  36. 36. Gosu var c = new SampleClass({"joe", "john", "jack"}) c.printNames("* ") * joe * john * jack print( c.Names ) [joe, john, jack]Wednesday, October 5, 11
  37. 37. Gosu uses java.util.List class SampleClass { var _names : List<String> construct( names : List<String> ) { _names = names } function printNames( prefix : String ) { for( n in _names ) { print( prefix + n ) } } property get Names() : List<String> { return _names } }Wednesday, October 5, 11
  38. 38. Gosu uses java.util.List class SampleClass { var _names : List<String> as Names construct( names : List<String> ) { _names = names } function printNames( prefix : String ) { for( n in _names ) { print( prefix + n ) } } }Wednesday, October 5, 11
  39. 39. Gosu uses java.util.List class SampleClass { var _names : List<String> as readonly Names construct( names : List<String> ) { _names = names } function printNames( prefix : String) { for( n in _names ) { print( prefix + n ) } } }Wednesday, October 5, 11
  40. 40. Gosu uses java.util.List class SampleClass { var _names : List<String> as readonly Names construct( names : List<String> ) { _names = names } function printNames( prefix : String = "> ") { for( n in _names ) { print( prefix + n ) } } }Wednesday, October 5, 11
  41. 41. Gosu class MyRunnable implements Runnable { delegate _runnable represents Runnable construct() { _runnable = new Runnable() { override function run() { print("Hello, Delegation") } } } property get Impl() : Runnable { return _runnable } property set Impl( r : Runnable ) { _runnable = r } }Wednesday, October 5, 11
  42. 42. Gosu class MyRunnable implements Runnable { delegate _runnable represents Runnable construct() { _runnable = new Runnable() { override function run() { print("Hello, Delegation") } } } property get Impl() : Runnable { return _runnable } property set Impl( r : Runnable ) { _runnable = r } } var x = new MyRunnable() x.Impl = new Runnable() { override function run() { print("Hello, Delegation 2") } }Wednesday, October 5, 11
  43. 43. Gosu - Donkey Patching Extensions enhancement MyStringEnhancement : String { function printMe() { print( this ) } } "Hello Enhancements".printMe() Enhancements are statically dispatched which means they cannot be used to implement interfaces or to achieve polymorphismWednesday, October 5, 11
  44. 44. Gosu Enhancements  String: rightPad(), leftPad(), center(), toDate(), toBoolean()  File: read(), write(), eachLine() Closures (Blocks) var listOfStrings = {"This", "is", "a", "list"} var longStrings = listOfStrings.where( s -> s.length>2) print( longStrings.join(", ") ) var r : Runnable r = -> print("This block was converted to a Runnable")Wednesday, October 5, 11
  45. 45. Gosu Now for the fun part.Wednesday, October 5, 11
  46. 46. Gosu Now for the fun part. XML marshalling!Wednesday, October 5, 11
  47. 47. Gosu Now for the fun part. XML marshalling! JAXB is “great” and all but it can be … painful.Wednesday, October 5, 11
  48. 48. Gosu Now for the fun part. XML marshalling! JAXB is “great” and all but it can be … painful. Gosu has great alternative.Wednesday, October 5, 11
  49. 49. Gosu Now for the fun part. XML marshalling! JAXB is “great” and all but it can be … painful. Gosu has great alternative. But first <shudder/> an XSD...Wednesday, October 5, 11
  50. 50. Gosu <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>Wednesday, October 5, 11
  51. 51. Gosu var bob = new myapp.example.Person() bob.Age = 32 bob.Firstname = "Bob" bob.Lastname = "Loblaw" bob.print()Wednesday, October 5, 11
  52. 52. Gosu var bob = new myapp.example.Person() bob.Age = 32 bob.Firstname = "Bob" bob.Lastname = "Loblaw" bob.print() What about WSDL?Wednesday, October 5, 11
  53. 53. Gosu var bob = new myapp.example.Person() bob.Age = 32 bob.Firstname = "Bob" bob.Lastname = "Loblaw" bob.print() // weather.wsdl var x = new myapp.weather() var forecast = x.GetCityForecastByZIP("95816") print( forecast.ForecastResult.Forecast.map( f -> f.Description ).join("n") )Wednesday, October 5, 11
  54. 54. Gosu Feature Literals var sub = String#substring(int) // class reference print( sub.invoke( "Now is the time for all good men", 2 ) ) // instance reference var sub = "Now is the time for all good men"#substring(int) print( sub.invoke( 2 ) )Wednesday, October 5, 11
  55. 55. Gosu Feature Literals var sub = String#substring(int) // class reference print( sub.invoke( "Now is the time for all good men", 2 ) ) // instance reference var sub = "Now is the time for all good men"#substring(2) print( sub.invoke() )Wednesday, October 5, 11
  56. 56. Gosu Feature Literals var sub = String#substring(int) // class reference print( sub.invoke( "Now is the time for all good men", 2 ) ) // instance reference var sub = "Now is the time for all good men"#substring(2) print( sub.invoke() ) var component = new Component(foo#Bar#Bob)Wednesday, October 5, 11
  57. 57. Gosu - Ronin <%@ extends ronin.RoninTemplate %> <%@ params(name : String) %> <html> <body> Hello ${name}! </body> </html>Wednesday, October 5, 11
  58. 58. Gosu - Ronin package controller uses ronin.* class Main extends RoninController { function hello(name : String) { view.Hello.render(writer, name) } }Wednesday, October 5, 11
  59. 59. Gosu  http://www.gosu-lang.org  Web framework http://ronin-web.org/  ORM: http://ronin-web.org/Tosa.html  Build tool: Aardvark http://vark.github.com/  JVM Language Summit: http://medianetwork.oracle.com/ media/show/17005  Twitter: @carson_gross  IRC: #gosulang @ irc.freenode.netWednesday, October 5, 11
  60. 60. Justin Lee, Oracle http://antwerkz.com @evanchooly http://github.com/evanchooly http://www.basementcoders.com | Copyright © 2011, Oracle and/or it’s affiliates. All rights Insert Information Protection Policy Classification from Slide 8 reserved. |Wednesday, October 5, 11

×