SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
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/

Contenu connexe

Tendances

The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtendtakezoe
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminarygo-lang
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Gostrikr .
 
Intro python-object-protocol
Intro python-object-protocolIntro python-object-protocol
Intro python-object-protocolShiyao Ma
 
Why my Go program is slow?
Why my Go program is slow?Why my Go program is slow?
Why my Go program is slow?Inada Naoki
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?Kevin Pilch
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on PythonSumit Raj
 

Tendances (20)

C++ L08-Classes Part1
C++ L08-Classes Part1C++ L08-Classes Part1
C++ L08-Classes Part1
 
The best of AltJava is Xtend
The best of AltJava is XtendThe best of AltJava is Xtend
The best of AltJava is Xtend
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
Kotlin
KotlinKotlin
Kotlin
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
Reflection in Go
Reflection in GoReflection in Go
Reflection in Go
 
C++ via C#
C++ via C#C++ via C#
C++ via C#
 
Intro python-object-protocol
Intro python-object-protocolIntro python-object-protocol
Intro python-object-protocol
 
Why my Go program is slow?
Why my Go program is slow?Why my Go program is slow?
Why my Go program is slow?
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 
devLink - What's New in C# 4?
devLink - What's New in C# 4?devLink - What's New in C# 4?
devLink - What's New in C# 4?
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
 

Similaire à Fantom - Programming Language for JVM, CLR, and Javascript

Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming languageSlawomir Dorzak
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Short intro to the Rust language
Short intro to the Rust languageShort intro to the Rust language
Short intro to the Rust languageGines Espada
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxSignalFx
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++Haripritha
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and VerificationKapilRaghunandanTrip
 
Python-GTK
Python-GTKPython-GTK
Python-GTKYuren Ju
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Tudor Dragan
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesEelco Visser
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixirbrien_wankel
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ applicationDaniele Pallastrelli
 

Similaire à Fantom - Programming Language for JVM, CLR, and Javascript (20)

Golang
GolangGolang
Golang
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Short intro to the Rust language
Short intro to the Rust languageShort intro to the Rust language
Short intro to the Rust language
 
Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFx
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Let's Go-lang
Let's Go-langLet's Go-lang
Let's Go-lang
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
C# programming
C# programming C# programming
C# programming
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 

Dernier

Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Dernier (20)

Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

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 = [ "fantom@opera.org", "fantom@gmail.com" ] }, Person { name = "Nobody"; emails = [ "nobody@nowhere.org", "noob@gmail.com" ] } ]
  • 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) } // -> fantom@opera.org
  • 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/