SlideShare a Scribd company logo
Fantom


Programming language for JVM, CLR, and JS



                                  2012, Kamil Toman
             http://fantomery.org/katox/fantom-en.pdf
What Is Fantom
●   Language for multiple platforms
●   Object oriented
●   Functional
●   Balance of static and dynamic typing
●   Well-known "c-like" syntax

● Opensource (AFL 3.0)
Productivity
●   Literals
●   Type inference
●   Functions
●   Closures
●   Mixins
●   Integration (FFI)
●   DSL support
Literals
● Lists
  [,]
  [ 10, 20, 30 ]
  [ "a", "b", [ "c", "d" ], "e" ]
● Maps
  [:]
  [ "x" : 10.0d, "y" : 3.14f ]
  [ 10 : "x", 20 : [ "p", "q", "r" ] ]
● Types (introspection)
  sys::Str#, Str#
● Slots (introspection)
  Str#plus, Int#plusDecimal, Uuid#fromStr
Literals (2)
● Uri
    `http://fantom.org`
    `http://myserver.com/edit?n=Ron&s=Smith`
    `/home/fantom/checkitout.fan`
● Duration
    1ns, 1ms, 1sec, 1min, 1hr, 1day
●   Range
    [ 10..20, 30..<40, -3..-1 ]
Type Inference
● u := Uuid()
    // u.typeof -> sys::Uuid
●   s := "some string"
    // s.typeof -> sys::Str
●   list := [10, 20, 30]
    // list.typeof -> sys::Int[]
    listObj := ["a", 1]
    // listObj.typeof -> sys::Obj[]
● map := ["x" : 10, "y" : 3]
    // map.typeof -> [sys::Str : sys::Int]
●   mapNum := ["x" : 10.0d, "y" : 3.14f]
    // mapNum.typeof -> [sys::Str : sys::Num]
Functions and Closures
● Functions - signature |A a, B b, ..., H h -> R|
    double := |Int a -> Int| { 2 * a }
    v := |->| {}
● Methods - just functions wrappers
    |Int a, Int b ->Int| plus := Int#plus.func
●   Closures - expressions that return functions
    Str prefix := ""
    print := |Obj o -> Str| { prefix + o.toStr }
●   Bind - bind parameters of existing functions
    op := |Method m, Int a, Int b| { m.callOn(a, [b]) }
    mul := opFunc.bind([Int#mult])             // |Int, Int->Int|
    plus2 := opFunc.bind([Int#plus, 2]) // |Int -> Int|
Mixins
mixin HasColor {
 abstract Str color
 virtual Void sayIt() { echo ("My favourite $color") } }

mixin HasAge {
 abstract Date produced
 virtual Bool isVeteran() { return produced < Date.fromStr("1919-01-01") } }

class Car : HasColor, HasAge {
 override Str color := "evil grey"
 override Date produced := Date.today
 override Void sayIt() { echo("My ${isVeteran ? "veteran" : color} car
produced ${produced.year}.") } }

Car().sayIt   // -> My evil grey car produced 2012.
Java FFI
● Java
  ○ package => Fantom pod
  ○ class => Fantom class
  ○ interface => Fantom mixin
  ○ field => Fantom field
  ○ method => Fantom method
   using [java] javax.swing
   using [java] java.util::Map$Entry as Entry
   f := JFrame(...)

● There are some limitations of Fantom FFI. What's not
   supported
   ○ overloaded methods, primitive multidimensional
      arrays, > 1 level deep inheritance from java classes
Javascript FFI
● source code Fantom -> Js generation (no bytecode)
   @Js
   class GonnaBeJs
   {
     Void sayHi() { Win.cur.alert("Hello!") }
   }

● native peer Js -> Fantom
    // Fantom
   class Foo {
       native Str? f
   }

    // Javascript
   fan.mypod.FooPeer.prototype.m_f = "";
   fan.mypod.FooPeer.prototype.f = function(t) { return this.m_f; }
   fan.mypod.FooPeer.prototype.f$ = function(t, v) { this.m_f = v; }
DSL Support
● Ability to integrate custom language or AST
    modifications of Fantom code
●   Syntax - DslType <|...|>
    echo(Str<|A towel, it says, is about the most massively useful thing an
    interstellar hitchhiker can have.|>)

    Regex<|^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$|>

    @Select
    User getUser(Int id){
      one(sql<|
       select u.userid, u.name
       from User u,Company c
       where u.id = #{id} and u.companid = c.id and c.isdeleted = 0
      |>)
    }
Practical Shortcuts
● Dynamic typing
    obj->foo // obj.trap("foo", [,])
●   Implicit upcast
    Derived derived := base
●   Optional return for simple expressions
    files.sort |a, b| { a.modified <=> b.modified }
●   isnot keyword
    alwaysFalse := "a" isnot Str
●   Optional parenthesis for functions of arity 0
    yes := "yes".capitalize
Practical Shortcuts (2)
● Optional function calls
    tel := contact?.phone?.mobile
●   Elvis operator
    x ?: def                 // x == null ? def : x
●   it-blocks
    `file.txt`.toFile.eachLine { echo (it) }
    // `file.txt`.toFile().eachLine(|line| { echo (line) })
●   literals for data
    Date.today - 1day // yesterday
●   string interpolation
    res.headers["Authorization"]="Basic " + "$user:$pass".
    toBuf.toBase64
Declarative Style
win := Window
  {
    size = Size(300,200)
    Label {
       text = "Hello world"
       halign=Halign.center },
  }.open

createTable("user") {
   createColumn("user_id", integer) { autoIncrement; primaryKey }
   createColumn("login", varchar(64)) { notNull; unique }
   createColumn("pwd", varchar(48)) { comment("Password hash"); notNull }
   createColumn("role_id", integer) { notNull }
   index("idx_login", ["login"])
   foreignKey("fk_role", ["role_id"], references("role", ["role_id"]))
}
Declarative Style (2)
class Person {
  Str? name
  Str[]? emails
}
phoneBook := [
   Person
   {
     name = "Fantom"; emails = [ "fantom@opera.org", "fantom@gmail.com" ]
   },
   Person
   {
     name = "Nobody"; emails = [ "nobody@nowhere.org", "noob@gmail.com" ]
   }
]
Functional Style
["ox", "cat", "deer", "whale"].map { it.size } // -> [2, 3, 4, 5]
[2,3,4].all { it > 1 }           // -> true
[2,3,4].any { it == 4 }          // -> true

["hello", 25, 3.14d, Time.now].findType(Num#) // -> [25, 3.14]
[1, 1, 2, 3, 2, 1, 1].findAll |i| { i.isEven } // -> [2, 2]

[1, 1, 2, 3, 2, 1, 1].reduce([:]) |[Int:Int]r, Int v->[Int:Int]| { return r[v]=r.get(v,0)+1 }
// -> [1:4, 2:2, 3:1]

phoneBook.findAll | Person p -> Bool | { p.name == "Fantom" }
            .map | Person p -> Str[] | { p.emails }
            .flatten
            .exclude | Str email -> Bool | { s.endsWith("gmail.com") }
            .each { echo (it) }
// -> fantom@opera.org
Safety
● Null & Not-Null types
● Const classes and fields
● Guaranteed functions/closures with no mutations
    (thread safe)
●   Actor system (no shared state)
    ○ Unsafe wrapper - break your rules whenever you
        want to save some time and shoot yourself into foot
Null and Not-Null types
● null assignment must be explicitly allowed in the code
● Default for types is not nullable version
● Automatic conversion
   ○ apparent bugs -> compile error
   ○ could work -> (maybe) runtime error
Null and Not-Null types (2)
class Nullable {                      // © Xored, Inc.
    Void main() {
         Int? nullable := null
         Int nonNullable := 0
         nonNullable = nullable            // runtime err
         nonNullable = null           // compile err
         do3(null)                    // compile err
         do3(nullable)                     // runtime err
    }
    Int do1(Int? arg) { null }  // compile err
    Int do2(Int? arg) { arg }   // runtime err
    Int? do3(Int arg) { arg }
}
Const
● Const classes guarantee their internal state won't
  change
● Const fields must be initialized on object construction
● Const fields must be const types or their values must be
  converted by calling toImmutable for List, Map, Func
Const
class NonConst {                     // © Xored, Inc.
    const Int constField
    Int nonConstField
    Void mutate() {
         constField = 4              // compile err
         nonConstField = 4           // ok
    }
}
class Const {
    new make(Int i, Str[] list) {
         // implicit call of list.toImmutable
         this.list = list
    }
    const Str[] list
}
Actor System
● Concurrency is not handled by thread but by Actors
● Actor is a const class extending concurrent::Actor
● Actors exchange immutable messages, i.e.
    a. serialized content
    b. constant content (allows to pass by reference)
●   Actors typically contain private data, which are mutable
    (thread local analogy)

    // asynchronous incrementation of number send (blocking send operation)
    actor:=Actor(ActorPool()) | Int msg -> Int | { msg + 1 }
    5.times { echo(actor.send(it).get) }
Tooling and Build System
● Out-of-the-box build system using Fantom
  ○ handles standard setup with no additional code
  ○ easy to understand (simple implementation)
  ○ module dependencies and version checking
● Bundled simple testing framework fant
  ○ fits into default build structure
  ○ pretty standard lifecycle (junit like)
  ○ works also for javascript targeted code
using build
class Build : BuildPod {
 new make() {
   podName = "mongo"; summary = "Interface to MongoDB (http://www.mongodb.com)"
   depends = ["sys 1.0", "inet 1.0", "concurrent 1.0"]
   srcDirs = [`test/`, `fan/`, `fan/gridfs/`, `fan/bson/`]
   docSrc = true } }
Modularity - Fantom repo & pods
// start all non-abstract MyService services in project CoolProj
using fanr
repo:=Repo.makeForUri(`http://my-fantom-repo/`)
pods:=repo.query(Str<|"*proj.name=="CoolProj"|>)
pods.each |pod|
{
  echo("$p.name$p.version$p.depends")
  pod.types.each |type|
  {
    if (type.fits(MyService#) && !type.isAbstract)
      type.make->start
  }
}
References
● Open source & docs
  ○ http://fantom.org/
  ○ http://www.talesframework.org/
  ○ http://www.fanzy.net/
  ○ http://langref.org/
  ○ http://rosettacode.org/wiki/Category:Fantom
● Commercial products
  ○ http://skyfoundry.com/skyspark/
  ○ http://www.xored.com/products/f4/
  ○ http://www.kloudo.com/
  ○ http://www.cull.io/

More Related Content

PDF
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest
 
PDF
Golang and Eco-System Introduction / Overview
Markus Schneider
 
PDF
Fun with functions
Frank Müller
 
PPT
Fantom on the JVM Devoxx09 BOF
Dror Bereznitsky
 
PPTX
Hacking Go Compiler Internals / GoCon 2014 Autumn
Moriyoshi Koizumi
 
PDF
Functional python
Jesué Junior
 
PDF
Антихрупкий TypeScript | Odessa Frontend Meetup #17
OdessaFrontend
 
PDF
Design Patterns in Modern C++
Dmitri Nesteruk
 
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest
 
Golang and Eco-System Introduction / Overview
Markus Schneider
 
Fun with functions
Frank Müller
 
Fantom on the JVM Devoxx09 BOF
Dror Bereznitsky
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Moriyoshi Koizumi
 
Functional python
Jesué Junior
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
OdessaFrontend
 
Design Patterns in Modern C++
Dmitri Nesteruk
 

What's hot (20)

PDF
C++ L08-Classes Part1
Mohammad Shaker
 
PDF
The best of AltJava is Xtend
takezoe
 
PPTX
Golang iran - tutorial go programming language - Preliminary
go-lang
 
PPTX
Kotlin
BoKaiRuan
 
PDF
MP in Clojure
Kent Ohashi
 
PDF
C++ L01-Variables
Mohammad Shaker
 
PDF
Go Lang Tutorial
Wei-Ning Huang
 
PDF
C++ L09-Classes Part2
Mohammad Shaker
 
PDF
Reflection in Go
strikr .
 
PPTX
C++ via C#
Egor Bogatov
 
PDF
Intro python-object-protocol
Shiyao Ma
 
PDF
Why my Go program is slow?
Inada Naoki
 
PDF
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 
PDF
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Muhammad Ulhaque
 
PDF
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
PDF
C++ L05-Functions
Mohammad Shaker
 
PDF
Free Monads Getting Started
Kent Ohashi
 
ODP
OpenGurukul : Language : C++ Programming
Open Gurukul
 
PPT
devLink - What's New in C# 4?
Kevin Pilch
 
ODP
Hands on Session on Python
Sumit Raj
 
C++ L08-Classes Part1
Mohammad Shaker
 
The best of AltJava is Xtend
takezoe
 
Golang iran - tutorial go programming language - Preliminary
go-lang
 
Kotlin
BoKaiRuan
 
MP in Clojure
Kent Ohashi
 
C++ L01-Variables
Mohammad Shaker
 
Go Lang Tutorial
Wei-Ning Huang
 
C++ L09-Classes Part2
Mohammad Shaker
 
Reflection in Go
strikr .
 
C++ via C#
Egor Bogatov
 
Intro python-object-protocol
Shiyao Ma
 
Why my Go program is slow?
Inada Naoki
 
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Muhammad Ulhaque
 
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
C++ L05-Functions
Mohammad Shaker
 
Free Monads Getting Started
Kent Ohashi
 
OpenGurukul : Language : C++ Programming
Open Gurukul
 
devLink - What's New in C# 4?
Kevin Pilch
 
Hands on Session on Python
Sumit Raj
 
Ad

Similar to Fantom - Programming Language for JVM, CLR, and Javascript (20)

PDF
Golang
Felipe Mamud
 
PDF
Introduction to Go programming language
Slawomir Dorzak
 
PPT
An Overview Of Python With Functional Programming
Adam Getchell
 
PPTX
C programming language tutorial
javaTpoint s
 
PDF
Short intro to the Rust language
Gines Espada
 
PDF
Go debugging and troubleshooting tips - from real life lessons at SignalFx
SignalFx
 
PPTX
programming language in c&c++
Haripritha
 
PDF
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
ODP
Writing MySQL UDFs
Roland Bouman
 
PPTX
C# 6.0 Preview
Fujio Kojima
 
PPTX
Lecture 04 Programming C for Beginners 001
MahmoudElsamanty
 
PDF
Python-GTK
Yuren Ju
 
PPTX
golang_getting_started.pptx
Guy Komari
 
PDF
Let's Go-lang
Luka Zakrajšek
 
PDF
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
PDF
TI1220 Lecture 14: Domain-Specific Languages
Eelco Visser
 
PPT
C# programming
umesh patil
 
DOCX
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
PDF
Introduction to Elixir
brien_wankel
 
PPTX
Java fundamentals
HCMUTE
 
Golang
Felipe Mamud
 
Introduction to Go programming language
Slawomir Dorzak
 
An Overview Of Python With Functional Programming
Adam Getchell
 
C programming language tutorial
javaTpoint s
 
Short intro to the Rust language
Gines Espada
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
SignalFx
 
programming language in c&c++
Haripritha
 
An Overview of SystemVerilog for Design and Verification
KapilRaghunandanTrip
 
Writing MySQL UDFs
Roland Bouman
 
C# 6.0 Preview
Fujio Kojima
 
Lecture 04 Programming C for Beginners 001
MahmoudElsamanty
 
Python-GTK
Yuren Ju
 
golang_getting_started.pptx
Guy Komari
 
Let's Go-lang
Luka Zakrajšek
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Tudor Dragan
 
TI1220 Lecture 14: Domain-Specific Languages
Eelco Visser
 
C# programming
umesh patil
 
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Introduction to Elixir
brien_wankel
 
Java fundamentals
HCMUTE
 
Ad

Recently uploaded (20)

PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Architecture of the Future (09152021)
EdwardMeyman
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 

Fantom - Programming Language for JVM, CLR, and Javascript

  • 1. Fantom Programming language for JVM, CLR, and JS 2012, Kamil Toman http://fantomery.org/katox/fantom-en.pdf
  • 2. What Is Fantom ● Language for multiple platforms ● Object oriented ● Functional ● Balance of static and dynamic typing ● Well-known "c-like" syntax ● Opensource (AFL 3.0)
  • 3. Productivity ● Literals ● Type inference ● Functions ● Closures ● Mixins ● Integration (FFI) ● DSL support
  • 4. Literals ● Lists [,] [ 10, 20, 30 ] [ "a", "b", [ "c", "d" ], "e" ] ● Maps [:] [ "x" : 10.0d, "y" : 3.14f ] [ 10 : "x", 20 : [ "p", "q", "r" ] ] ● Types (introspection) sys::Str#, Str# ● Slots (introspection) Str#plus, Int#plusDecimal, Uuid#fromStr
  • 5. Literals (2) ● Uri `http://fantom.org` `http://myserver.com/edit?n=Ron&s=Smith` `/home/fantom/checkitout.fan` ● Duration 1ns, 1ms, 1sec, 1min, 1hr, 1day ● Range [ 10..20, 30..<40, -3..-1 ]
  • 6. Type Inference ● u := Uuid() // u.typeof -> sys::Uuid ● s := "some string" // s.typeof -> sys::Str ● list := [10, 20, 30] // list.typeof -> sys::Int[] listObj := ["a", 1] // listObj.typeof -> sys::Obj[] ● map := ["x" : 10, "y" : 3] // map.typeof -> [sys::Str : sys::Int] ● mapNum := ["x" : 10.0d, "y" : 3.14f] // mapNum.typeof -> [sys::Str : sys::Num]
  • 7. Functions and Closures ● Functions - signature |A a, B b, ..., H h -> R| double := |Int a -> Int| { 2 * a } v := |->| {} ● Methods - just functions wrappers |Int a, Int b ->Int| plus := Int#plus.func ● Closures - expressions that return functions Str prefix := "" print := |Obj o -> Str| { prefix + o.toStr } ● Bind - bind parameters of existing functions op := |Method m, Int a, Int b| { m.callOn(a, [b]) } mul := opFunc.bind([Int#mult]) // |Int, Int->Int| plus2 := opFunc.bind([Int#plus, 2]) // |Int -> Int|
  • 8. Mixins mixin HasColor { abstract Str color virtual Void sayIt() { echo ("My favourite $color") } } mixin HasAge { abstract Date produced virtual Bool isVeteran() { return produced < Date.fromStr("1919-01-01") } } class Car : HasColor, HasAge { override Str color := "evil grey" override Date produced := Date.today override Void sayIt() { echo("My ${isVeteran ? "veteran" : color} car produced ${produced.year}.") } } Car().sayIt // -> My evil grey car produced 2012.
  • 9. Java FFI ● Java ○ package => Fantom pod ○ class => Fantom class ○ interface => Fantom mixin ○ field => Fantom field ○ method => Fantom method using [java] javax.swing using [java] java.util::Map$Entry as Entry f := JFrame(...) ● There are some limitations of Fantom FFI. What's not supported ○ overloaded methods, primitive multidimensional arrays, > 1 level deep inheritance from java classes
  • 10. Javascript FFI ● source code Fantom -> Js generation (no bytecode) @Js class GonnaBeJs { Void sayHi() { Win.cur.alert("Hello!") } } ● native peer Js -> Fantom // Fantom class Foo { native Str? f } // Javascript fan.mypod.FooPeer.prototype.m_f = ""; fan.mypod.FooPeer.prototype.f = function(t) { return this.m_f; } fan.mypod.FooPeer.prototype.f$ = function(t, v) { this.m_f = v; }
  • 11. DSL Support ● Ability to integrate custom language or AST modifications of Fantom code ● Syntax - DslType <|...|> echo(Str<|A towel, it says, is about the most massively useful thing an interstellar hitchhiker can have.|>) Regex<|^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$|> @Select User getUser(Int id){ one(sql<| select u.userid, u.name from User u,Company c where u.id = #{id} and u.companid = c.id and c.isdeleted = 0 |>) }
  • 12. Practical Shortcuts ● Dynamic typing obj->foo // obj.trap("foo", [,]) ● Implicit upcast Derived derived := base ● Optional return for simple expressions files.sort |a, b| { a.modified <=> b.modified } ● isnot keyword alwaysFalse := "a" isnot Str ● Optional parenthesis for functions of arity 0 yes := "yes".capitalize
  • 13. Practical Shortcuts (2) ● Optional function calls tel := contact?.phone?.mobile ● Elvis operator x ?: def // x == null ? def : x ● it-blocks `file.txt`.toFile.eachLine { echo (it) } // `file.txt`.toFile().eachLine(|line| { echo (line) }) ● literals for data Date.today - 1day // yesterday ● string interpolation res.headers["Authorization"]="Basic " + "$user:$pass". toBuf.toBase64
  • 14. Declarative Style win := Window { size = Size(300,200) Label { text = "Hello world" halign=Halign.center }, }.open createTable("user") { createColumn("user_id", integer) { autoIncrement; primaryKey } createColumn("login", varchar(64)) { notNull; unique } createColumn("pwd", varchar(48)) { comment("Password hash"); notNull } createColumn("role_id", integer) { notNull } index("idx_login", ["login"]) foreignKey("fk_role", ["role_id"], references("role", ["role_id"])) }
  • 15. Declarative Style (2) class Person { Str? name Str[]? emails } phoneBook := [ Person { name = "Fantom"; emails = [ "[email protected]", "[email protected]" ] }, Person { name = "Nobody"; emails = [ "[email protected]", "[email protected]" ] } ]
  • 16. Functional Style ["ox", "cat", "deer", "whale"].map { it.size } // -> [2, 3, 4, 5] [2,3,4].all { it > 1 } // -> true [2,3,4].any { it == 4 } // -> true ["hello", 25, 3.14d, Time.now].findType(Num#) // -> [25, 3.14] [1, 1, 2, 3, 2, 1, 1].findAll |i| { i.isEven } // -> [2, 2] [1, 1, 2, 3, 2, 1, 1].reduce([:]) |[Int:Int]r, Int v->[Int:Int]| { return r[v]=r.get(v,0)+1 } // -> [1:4, 2:2, 3:1] phoneBook.findAll | Person p -> Bool | { p.name == "Fantom" } .map | Person p -> Str[] | { p.emails } .flatten .exclude | Str email -> Bool | { s.endsWith("gmail.com") } .each { echo (it) } // -> [email protected]
  • 17. Safety ● Null & Not-Null types ● Const classes and fields ● Guaranteed functions/closures with no mutations (thread safe) ● Actor system (no shared state) ○ Unsafe wrapper - break your rules whenever you want to save some time and shoot yourself into foot
  • 18. Null and Not-Null types ● null assignment must be explicitly allowed in the code ● Default for types is not nullable version ● Automatic conversion ○ apparent bugs -> compile error ○ could work -> (maybe) runtime error
  • 19. Null and Not-Null types (2) class Nullable { // © Xored, Inc. Void main() { Int? nullable := null Int nonNullable := 0 nonNullable = nullable // runtime err nonNullable = null // compile err do3(null) // compile err do3(nullable) // runtime err } Int do1(Int? arg) { null } // compile err Int do2(Int? arg) { arg } // runtime err Int? do3(Int arg) { arg } }
  • 20. Const ● Const classes guarantee their internal state won't change ● Const fields must be initialized on object construction ● Const fields must be const types or their values must be converted by calling toImmutable for List, Map, Func
  • 21. Const class NonConst { // © Xored, Inc. const Int constField Int nonConstField Void mutate() { constField = 4 // compile err nonConstField = 4 // ok } } class Const { new make(Int i, Str[] list) { // implicit call of list.toImmutable this.list = list } const Str[] list }
  • 22. Actor System ● Concurrency is not handled by thread but by Actors ● Actor is a const class extending concurrent::Actor ● Actors exchange immutable messages, i.e. a. serialized content b. constant content (allows to pass by reference) ● Actors typically contain private data, which are mutable (thread local analogy) // asynchronous incrementation of number send (blocking send operation) actor:=Actor(ActorPool()) | Int msg -> Int | { msg + 1 } 5.times { echo(actor.send(it).get) }
  • 23. Tooling and Build System ● Out-of-the-box build system using Fantom ○ handles standard setup with no additional code ○ easy to understand (simple implementation) ○ module dependencies and version checking ● Bundled simple testing framework fant ○ fits into default build structure ○ pretty standard lifecycle (junit like) ○ works also for javascript targeted code using build class Build : BuildPod { new make() { podName = "mongo"; summary = "Interface to MongoDB (http://www.mongodb.com)" depends = ["sys 1.0", "inet 1.0", "concurrent 1.0"] srcDirs = [`test/`, `fan/`, `fan/gridfs/`, `fan/bson/`] docSrc = true } }
  • 24. Modularity - Fantom repo & pods // start all non-abstract MyService services in project CoolProj using fanr repo:=Repo.makeForUri(`http://my-fantom-repo/`) pods:=repo.query(Str<|"*proj.name=="CoolProj"|>) pods.each |pod| { echo("$p.name$p.version$p.depends") pod.types.each |type| { if (type.fits(MyService#) && !type.isAbstract) type.make->start } }
  • 25. References ● Open source & docs ○ http://fantom.org/ ○ http://www.talesframework.org/ ○ http://www.fanzy.net/ ○ http://langref.org/ ○ http://rosettacode.org/wiki/Category:Fantom ● Commercial products ○ http://skyfoundry.com/skyspark/ ○ http://www.xored.com/products/f4/ ○ http://www.kloudo.com/ ○ http://www.cull.io/