Fantom on the JVM Devoxx09 BOF

1,940
-1

Published on

Fantom on the JVM Devoxx09 BOF

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,940
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
32
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Fantom on the JVM Devoxx09 BOF

  1. 1. Fantom on the JVM By Fred Simon, JFrog Dror Bereznitsky, AlphaCSP Stephen Colebourne, Joda
  2. 2. <ul><li>A BOF about the Fantom language </li></ul>
  3. 3. Agenda <ul><li>Introduction to Fantom </li></ul><ul><li>Why Fantom ? </li></ul><ul><li>The future of Fantom </li></ul>
  4. 4. Speaker’s Qualifications <ul><li>Fred Simon is a Founder and Chief Architect at JFrog Ltd. </li></ul><ul><li>Dror Bereznitsky is the Director of Technologies at AlphaCSP Ltd. </li></ul><ul><li>Stephen Colebourne is the Joda project lead </li></ul>
  5. 5. Why Fantom ? <ul><li>A practical programming language </li></ul><ul><ul><li>Hybrid OO and functional language </li></ul></ul><ul><ul><li>Static typing with dynamic abilities </li></ul></ul><ul><ul><li>Elegant APIs </li></ul></ul><ul><ul><li>Modularity baked into the language </li></ul></ul><ul><ul><li>Modern approach to concurrency </li></ul></ul><ul><ul><li>RESTFul design </li></ul></ul><ul><ul><li>JSON style serialization </li></ul></ul>
  6. 6. Hello World <ul><li>class HelloWorld { </li></ul><ul><li>static Void main() { </li></ul><ul><li>echo( “Hello world !&quot; ) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Pods <ul><li>Pods are used to organize types </li></ul><ul><ul><li>Like Java packages or C# namespaces </li></ul></ul><ul><ul><li>Qualified type names - <pod>::<type> </li></ul></ul><ul><ul><ul><li>sys::DateTime </li></ul></ul></ul><ul><ul><ul><li>web::Weblet </li></ul></ul></ul>
  8. 8. Pod Declaration – pod.fan <ul><li>@podDepends = [Depend( &quot; sys 1.0 &quot; ), Depend( &quot; someOtherPod 1.0 &quot; )] </li></ul><ul><li>@podSrcDirs = [ `fan/` , `test/` ] </li></ul><ul><li>pod myPod { </li></ul><ul><li>// symbols </li></ul><ul><li>Str mySymbol := &quot; value &quot; </li></ul><ul><li>} </li></ul>
  9. 9. Pods <ul><li>Unit of deployment and versioning </li></ul><ul><ul><li>Similar to a Java JAR file or a .NET assembly </li></ul></ul><ul><ul><li>Pod names must be globally unique </li></ul></ul><ul><li>Standard ZIP file </li></ul><ul><ul><li>Bundles Fantom code, meta-data, and file resources </li></ul></ul>
  10. 10. Type System <ul><li>Two kind of types </li></ul><ul><ul><li>Classes </li></ul></ul><ul><ul><li>Mixins </li></ul></ul><ul><li>Everything is an object </li></ul><ul><ul><li>Value types – Bool, Int, Float </li></ul></ul>
  11. 11. Classes <ul><li>Single class inheritance </li></ul><ul><li>May inherit multiple Mixins </li></ul><ul><li>The root of all classes is the sys::Obj </li></ul><ul><ul><li>class Foo : Bar { } </li></ul></ul>
  12. 12. Mixins <ul><li>Similar Scala traits </li></ul><ul><li>Java/C# interfaces + method implementation </li></ul><ul><li>No state </li></ul><ul><li>mixin Foo { </li></ul><ul><li>Int bar() { return 42 } </li></ul><ul><li>} </li></ul>
  13. 13. Enums <ul><li>Enums are normal classes </li></ul><ul><li>Enums have a fixed range of instances </li></ul><ul><li>enum Color { red, blue, green } </li></ul>
  14. 14. Null Safety <ul><li>Types may be nullable or non-nullable </li></ul><ul><ul><li>By default types are non-nullable </li></ul></ul><ul><ul><li>Use ? to specify a nullable type </li></ul></ul><ul><li>Int? x := null </li></ul><ul><ul><li>Int x := null // Error </li></ul></ul>
  15. 15. Generics <ul><li>No user defined generics </li></ul><ul><li>Limited to List, Map, and Func Str[] // list of Str [ Str:User] // map of Users keyed by Str |Int a->Str| // takes Int, returns Str </li></ul>
  16. 16. Why Fantom? <ul><li>Built in modularity </li></ul><ul><li>Everything is an Object </li></ul><ul><ul><li>Value-types for performance </li></ul></ul><ul><li>Statically typed mixins </li></ul><ul><li>Null safety </li></ul><ul><li>No user defined generics </li></ul>
  17. 17. Slots <ul><li>Fields and Methods on Types </li></ul><ul><li>Uniquely named </li></ul><ul><ul><li>No overloading </li></ul></ul><ul><li>Protection Scope </li></ul><ul><ul><li>public (default) </li></ul></ul><ul><ul><li>private </li></ul></ul><ul><ul><li>protected </li></ul></ul><ul><ul><li>internal - only accessible inside pod </li></ul></ul>
  18. 18. Slots - Methods <ul><li>Not virtual by default </li></ul><ul><ul><li>Must explicitly specify virtual and override </li></ul></ul><ul><ul><li>class Animal { </li></ul></ul><ul><ul><li>virtual Void talk() { echo( &quot;generic&quot; ) } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>class Cat : Animal { </li></ul></ul><ul><ul><li>override Void talk() { echo( &quot;meow&quot; ) } </li></ul></ul><ul><ul><li>} </li></ul></ul>
  19. 19. Slots – Methods Continued <ul><li>Default parameters </li></ul><ul><ul><li>Void foo(Int a, Int b := 5 ) </li></ul></ul><ul><li>Once methods </li></ul><ul><ul><li>Computed first time, then cached </li></ul></ul><ul><ul><li>once Str fullName() { </li></ul></ul><ul><ul><li>return &quot;$firstName $lastName&quot; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Slots - Fields <ul><li>Use the := operator to provide an initial value Str message := &quot;hello world&quot; </li></ul><ul><li>Implicitly use a getter/setter </li></ul><ul><li>Use * to access field storage directly </li></ul><ul><li>*message := &quot;hello world&quot; </li></ul>
  21. 21. Slots – Field Accessors <ul><li>Accessors </li></ul><ul><ul><li>Int x := 10 { get { return *x } set { *x = val } } </li></ul></ul><ul><li>Can narrow field setter scope </li></ul><ul><li>Int x { private set } readonly Int x // the same </li></ul>
  22. 22. Slots – Field Immutability <ul><li>const - indicate that the field is immutable </li></ul><ul><li>Cannot be assigned outside of the construction process </li></ul><ul><li>Value of all const fields must be immutable </li></ul><ul><ul><li>Any object instance of a const class </li></ul></ul><ul><ul><li>The result of Obj.toImmutable (1.0.47) </li></ul></ul>
  23. 23. Constructors <ul><li>Multiple constructors allowed </li></ul><ul><ul><li>Use the new keyword </li></ul></ul><ul><ul><li>Convention is makeXXX() </li></ul></ul><ul><ul><li>Implicit make() constructor if not defined new make(Str name := &quot;anonymous&quot; ){ this .name = name </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>new makeFoo(Int x) { this .x = x } </li></ul></ul>
  24. 24. Calling Constructors <ul><li>Called just like static factory methods foo := Foo.make </li></ul><ul><li>foo := Foo.make( &quot;bar&quot; ) </li></ul><ul><li>foo := Foo.makeFoo( 5 ) </li></ul>
  25. 25. Construction Calls <ul><li>Special construction syntax: Type(args) </li></ul><ul><li>Bind to Type.fromStr if exists and takes a single Str argument </li></ul><ul><li>Otherwise bind to Type.make foo := Foo.make // non-preferred </li></ul><ul><li>foo := Foo() // preferred </li></ul>
  26. 26. Why Fantom ? <ul><li>No overloading </li></ul><ul><ul><li>Simple reflection </li></ul></ul><ul><ul><li>Method resolution with default parameters </li></ul></ul><ul><li>Implicit getter and setters </li></ul><ul><li>Immutability </li></ul><ul><li>Human readable serialization </li></ul>
  27. 27. Literals Int 45 -89_039 0xCAFE_BABE Float 3.0f 3f 0.2e+6f Decimal 4d 4.0 0.2e+6D Str “ foo” “x is $x, in hex $x.toHex” Duration 4ns 100ms -0.5hr Uri `index.html` ` http://fandev.org ` Types, Slots Str# sys::Str# Int#plus #echo
  28. 28. Literals Continued Range 0..5 x...y List [1, 2, 3] Int[10, 20, 30] Int[,] Map [1:”one”, 2:”two”] Int:Str[1:”one”] Int:Str[:]
  29. 29. Str Literals <ul><li>Multiline </li></ul><ul><ul><li>x := &quot;line 1 </li></ul></ul><ul><ul><li>line 2 </li></ul></ul><ul><ul><li>line3&quot; </li></ul></ul><ul><li>Triple quotes </li></ul><ul><ul><li>echo( &quot;&quot;&quot;Do you know &quot;What lies beneath the shadow of the statue&quot;?&quot;&quot;&quot; ) </li></ul></ul><ul><li>Interpolation </li></ul><ul><ul><li>&quot;x is $x , in hex $x.toHex , and x+8 is ${x+8} &quot; </li></ul></ul>
  30. 30. Functions <ul><li>First class objects - sys::Func </li></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><li>Closures </li></ul></ul><ul><ul><li>Bind </li></ul></ul><ul><li>Signature – </li></ul><ul><li>|A a, B b ... H h -> R| </li></ul><ul><ul><li>|Int a| </li></ul></ul><ul><ul><li>|Str a, Str b -> Int| </li></ul></ul>
  31. 31. Functions <ul><li>Calling </li></ul><ul><ul><li>func.callList([ 7 , 8 ]) </li></ul></ul><ul><ul><li>func.call( 7 , 8 ) </li></ul></ul><ul><ul><li>func( 7 , 8 ) </li></ul></ul>
  32. 32. Methods <ul><li>Wrapper around a Func </li></ul><ul><li>Method.func </li></ul><ul><ul><li>func := Str#replace.func </li></ul></ul><ul><ul><li>func( &quot;hi!&quot; , &quot;hi&quot; , &quot;hello&quot; ) </li></ul></ul><ul><ul><li>&quot;hi!&quot; .replace( &quot;hi&quot; , &quot;hello&quot; ) </li></ul></ul>
  33. 33. Func.bind <ul><li>Binds one or more fixed arguments to create a new function </li></ul><ul><ul><li>f := |Int a, Int b-> Str| { </li></ul></ul><ul><ul><li>return &quot;$a $b&quot; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>g := f.bind([ 0 ]) </li></ul></ul><ul><ul><li>f( 0 , 1 ) // 0 1 </li></ul></ul><ul><ul><li>g( 1 ) // 0 1 </li></ul></ul>
  34. 34. Closures <ul><li>Functions that can reference enclosing scope </li></ul><ul><ul><li>counter := 0 </li></ul></ul><ul><ul><li>// counter = 1 </li></ul></ul><ul><ul><li>f := |->Int| { return ++counter } f() </li></ul></ul><ul><ul><li>f() // counter = 2 </li></ul></ul>
  35. 35. Closures <ul><li>Last arg to a method, can be defined “outside” </li></ul><ul><ul><li>// not preferred </li></ul></ul><ul><ul><li>foo( 5 , |->Int| { x }) </li></ul></ul><ul><ul><li>// preferred </li></ul></ul><ul><ul><li>foo( 5 ) |->Int| { x } </li></ul></ul>
  36. 36. Closures <ul><li>Used extensively in the API </li></ul><ul><ul><li>[ 1 , 2 , 3 ].each |Int i| {echo(i)} </li></ul></ul><ul><ul><li>[ 3 , 8 , 4 ].sort |Int a, Int b->Int| { a <=> b } </li></ul></ul>
  37. 37. It Blocks <ul><li>Syntax sugar to bind this to another variable named it </li></ul><ul><li>foo := Foo() { age = 15 } </li></ul><ul><li>foo := Foo(); </li></ul><ul><li>foo.age = 15 </li></ul><ul><li>Can set const fields </li></ul>
  38. 38. Dynamic Invoke <ul><li>Dynamic invoke operator: -> </li></ul><ul><li>No compile time type checking </li></ul><ul><li>Actually invokes Obj.trap() </li></ul><ul><ul><li>By default trap() uses reflection to lookup and call method a->x a.trap( “x” , [,]) a->x = b a.trap( “x” , [b]) a->x(b, c) a.trap( “x” , [b, c]) </li></ul></ul>
  39. 39. Why Fantom ? <ul><li>Functions are 1 st class citizens </li></ul><ul><li>Statically typed, duck typing possible </li></ul><ul><li>Declarative style with blocks </li></ul><ul><li>Built-in immutable calls </li></ul>
  40. 40. Facets <ul><li>Annotate types/slots with meta-data </li></ul><ul><li>Similar to Java annotations or C# attributes </li></ul><ul><li>@symbolName=value </li></ul><ul><ul><li>@serializable </li></ul></ul><ul><ul><li>@icon= `/icons/account.png` </li></ul></ul><ul><ul><li>@version=Version( &quot;1.2&quot; ) </li></ul></ul>
  41. 41. Facets <ul><li>Access at runtime using </li></ul><ul><ul><li>Pod.facet </li></ul></ul><ul><ul><li>Type.facet </li></ul></ul><ul><ul><li>Slot.facet </li></ul></ul><ul><li>obj.type.facet(@simple) == true </li></ul>
  42. 42. Symbols <ul><li>pod fanDemo </li></ul><ul><li>{ </li></ul><ul><li>Str defaultName := &quot;anonymous“ </li></ul><ul><li>} </li></ul><ul><li>class Person { </li></ul><ul><li>const Str firstName </li></ul><ul><li>const Str lastName </li></ul><ul><li>new make(Str firstName := @defaultName, Str lastName := &quot;&quot; ) { </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  43. 43. DSL <ul><li>Embed other languages into your Fantom source </li></ul><ul><li>Built in DSLs </li></ul><ul><ul><li>Str <|no or $ escapes need, and multi-line works too|> </li></ul></ul><ul><ul><li>Regex <|foo|foo/(d*)|> </li></ul></ul><ul><ul><li>DSL Plugins </li></ul></ul>
  44. 44. Concurrency <ul><li>No mutable shared state </li></ul><ul><ul><li>Threads may never share mutable state under any circumstances </li></ul></ul><ul><li>Immutability </li></ul><ul><li>Message passing </li></ul><ul><li>REST </li></ul><ul><ul><li>UriSpaces - a whiteboard mechanism for multiple threads to share state </li></ul></ul>
  45. 45. Immutability <ul><li>Immutable object = once constructed it never changes state </li></ul><ul><ul><li>Any object instance of a const class </li></ul></ul><ul><ul><li>The result of Obj.toImmutable() </li></ul></ul>
  46. 46. Immutability - Const Classes <ul><li>Use const keyword </li></ul><ul><ul><li>const class Foo {} </li></ul></ul><ul><li>Can only contain const fields or fields with no storage </li></ul><ul><ul><li>Use const keyword - const Int id := 5 </li></ul></ul><ul><ul><li>Can only be set in the constructor </li></ul></ul><ul><li>Must inherit from a const class or sys::Obj </li></ul>
  47. 47. Message Passing - Actors <ul><li>pool := ActorPool() </li></ul><ul><li>a := Actor(pool) |Obj msg| { </li></ul><ul><li>echo( &quot;$Time.now: $msg&quot; ) </li></ul><ul><li>} </li></ul><ul><li>a.send( &quot;start&quot; ) </li></ul><ul><li>a.sendLater( 1sec , &quot;1sec&quot; ) </li></ul>
  48. 48. Serialization <ul><li>Syntax is a subset of the Fantom grammar </li></ul><ul><ul><li>myPod::Person { </li></ul></ul><ul><ul><li>name = &quot;Homer Simpson&quot; </li></ul></ul><ul><ul><li>age = 39 </li></ul></ul><ul><ul><li>children = </li></ul></ul><ul><ul><li>[ &quot;Bart&quot; , &quot;Lisa&quot; , &quot;Maggie&quot; ] </li></ul></ul><ul><ul><li>} </li></ul></ul>
  49. 49. Serialization <ul><li>Tree based </li></ul><ul><ul><li>No attempt to keep track of object references </li></ul></ul><ul><ul><li>Tree nodes </li></ul></ul><ul><ul><ul><li>Literal </li></ul></ul></ul><ul><ul><ul><li>Simple - serialized via string representation </li></ul></ul></ul><ul><ul><ul><li>Complex - aggregate of literal, simple, and/or complex </li></ul></ul></ul>
  50. 50. Serialization - Simple <ul><li>Types that are serializable to/from a string </li></ul><ul><li>toStr() must return suitable string representation </li></ul><ul><li>Must have static fromStr() method </li></ul>
  51. 51. Serialization - Simple <ul><li>@simple </li></ul><ul><li>class Point { </li></ul><ul><li>static Point fromStr(Str s) { </li></ul><ul><li>t := s.split( &quot;,&quot; ); </li></ul><ul><li>return make(t[ 0 ].toInt, t[ 1 ].toInt) </li></ul><ul><li>} </li></ul><ul><li>override Str toStr() { return &quot;$x,$y&quot; } </li></ul><ul><li>Int x := 0 </li></ul><ul><li>Int y := 0 </li></ul><ul><li>} </li></ul>
  52. 52. Serialization - Complex <ul><li>@serializable facet </li></ul><ul><li>By default will serialize all non-static fields </li></ul><ul><li>Use @transient to skip fields </li></ul><ul><ul><li>@serializable </li></ul></ul><ul><ul><li>class Rectangle { Int x; Int y Int w; Int h @transient Int area } </li></ul></ul>
  53. 53. Serialization - @collections <ul><li>Nicer syntax for nesting children </li></ul><ul><ul><li>Person { </li></ul></ul><ul><ul><li>name = &quot;Homer Simpson&quot; </li></ul></ul><ul><ul><li>Person { name = &quot;Bart&quot; } </li></ul></ul><ul><ul><li>Person { name = &quot;Lisa&quot; } </li></ul></ul><ul><ul><li>Person { name = &quot;Maggie&quot; } </li></ul></ul><ul><ul><li>} </li></ul></ul>
  54. 54. Java FFI <ul><ul><li>using [java] java.util::Date as JavaDate </li></ul></ul><ul><ul><li>class Test { </li></ul></ul><ul><ul><li>Void main() { </li></ul></ul><ul><ul><li>date := JavaDate() // constructors </li></ul></ul><ul><ul><li>year := date.getYear + 1900 // methods </li></ul></ul><ul><ul><li>// dynamic invokes </li></ul></ul><ul><ul><li>millis := date->getTime echo( </li></ul></ul><ul><ul><li>&quot;$date, $year, $millis&quot; ) </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  55. 55. UriSpaces <ul><li>Standard ‘RESTful’ URI naming system </li></ul><ul><ul><li>Resources are represented as Fantom objects </li></ul></ul><ul><ul><li>Resources are identified with Uris </li></ul></ul><ul><ul><li>Resources are managed using a small set of &quot;verbs&quot; </li></ul></ul><ul><ul><li>Resources are transferred between threads safely </li></ul></ul>
  56. 56. UriSpaces <ul><li>UriSpace.root.create( `/hello` , &quot;hi&quot; ) </li></ul><ul><li>UriSpace.root[ `/hello` ] // &quot;hi“ </li></ul><ul><li>UriSpace.root.put( `/hello` , &quot;hola&quot; ) </li></ul><ul><li>UriSpace.root[ `/hello` ] // &quot;hola“ </li></ul><ul><li>UriSpace.root.delete( `/hello` ) </li></ul><ul><li>UriSpace.root[ `/hello` ] // raises UnresolvedErr </li></ul><ul><li>UriSpace.root.get( `/hello` , false ) // null </li></ul>
  57. 57. Type Database <ul><li>Maintains database of all installed pods and types </li></ul><ul><li>Provide a mechanism to index facet name/value pairs to types </li></ul><ul><li>Automatically rebuilt as needed </li></ul>
  58. 58. Type Database – Facet Indexing <ul><li>@podIndexFacets = [@parserForExt] </li></ul><ul><li>pod acme { </li></ul><ul><li>Str[] parserForExt := Str[,] </li></ul><ul><li>} </li></ul><ul><li>@parserForExt=[ &quot;xml&quot; ] </li></ul><ul><li>class XmlParser {} </li></ul><ul><li>parsers := Type.findByFacet(@parserForExt, &quot;xml&quot; ) </li></ul>
  59. 59. Build <ul><li>using build </li></ul><ul><li>class Build : BuildPod { </li></ul><ul><li>override Void setup() { </li></ul><ul><li>podName = “myPodName&quot; </li></ul><ul><li>version = Version( &quot;1.0&quot; ) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  60. 60. Build – Custom Code <ul><li>using build </li></ul><ul><li>class Build : BuildScript { </li></ul><ul><li>@target= &quot;Compile everything&quot; </li></ul><ul><li>Void compile() { log.info( &quot;Compile away!&quot; ) } </li></ul><ul><li>@target= &quot;Clean it all up&quot; </li></ul><ul><li>Void clean() { log.info( &quot;Clean away!&quot; ) } </li></ul><ul><li>} </li></ul>
  61. 61. Fandoc <ul><li>Plaint-text documentation language </li></ul><ul><ul><li>Inline code: 'FandocParser' </li></ul></ul><ul><ul><li>Emphasis: *foo bar* </li></ul></ul><ul><ul><li>Hyperlink: [Fantom Home Page]` http://fandev.org/` </li></ul></ul><ul><li>Based on Markdown </li></ul>
  62. 62. Deployment <ul><li>Fantom shipped as fcode in pods </li></ul><ul><li>At runtime, fcode is emitted to Java bytecode or MSIL </li></ul><ul><li>Sys API is implemented natively in Java/C# </li></ul>
  63. 63. Why Fantom ? <ul><li>Concurrency – state isolation </li></ul><ul><li>Type database </li></ul><ul><ul><li>No more scanning for annotations </li></ul></ul><ul><li>Easy integration with Java </li></ul><ul><li>Out of the box build system </li></ul><ul><li>Provided test framework </li></ul><ul><ul><li>No more modifiers trick with dynamic calls </li></ul></ul>
  64. 64. DEMO <ul><li>What’sthis demo about </li></ul>
  65. 65. Thanks for your attention! <ul><li>http://www.fantom.org </li></ul><ul><li>http://wiki.jfrog.org/confluence/display/FANP/Home </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×