60
Fantom

Fantom and Tales

Embed Size (px)

DESCRIPTION

Presentation at JUG-chennai on Fantom and Tales

Citation preview

Page 1: Fantom and Tales

Fantom

Page 2: Fantom and Tales

Fantom is boring - Brian frank

BY DESIGNcreator of

fantom(along with andy frank)

Page 3: Fantom and Tales

It’s about damn timeWe had a boring language

Page 4: Fantom and Tales

JavaLet’s start with

Page 5: Fantom and Tales

Another language?Why do we need

Page 6: Fantom and Tales

Java was looking great in 1995

Page 7: Fantom and Tales

In 2011, still beautiful just a little old

Page 8: Fantom and Tales

Java Better?

What would you change to make

Page 9: Fantom and Tales

Lets keep the compiler

Page 10: Fantom and Tales

THE LITTLE CHANGES

Page 11: Fantom and Tales

Remove semi-colens

int a = 0 ; int b = 2 ;

int a = 0

int b = 2

Page 12: Fantom and Tales

String interpolationString event = “jug”;String host = “Raj”;

Sop(“Welcome to ‘“ + event + “’ by “ + host)

Sop(“Welcome to ‘$event’ by $host “)

Page 13: Fantom and Tales

Type inference

String event = “Jug Meeting”

event = “Jug Meeting”

Page 14: Fantom and Tales

Multiline strings

String query = """ select * from users where company=‘microsoft’ and userid

in(select id from usersalary where sal > 200000000)

"""

String query = “select * from users where” + “company=‘microsoft’ and ” + “ userid in(select id from usersalary” +

“where sal > 200000000)”

Page 15: Fantom and Tales

echo

System.out.println(“hi”)

echo(“hi”)

Page 16: Fantom and Tales

Everything’s an object

3.toString()

3.times{ echo(“hi”)}

Page 17: Fantom and Tales

Non null-able fields

Int a = 3 Int? b = 3

a = null //compiler error

Page 18: Fantom and Tales

Less noisy ‘list’ syntax

User[] users = new User[]

//Array style index accessusers[0] = new User();

//List style auto grow and stuffusers.add(new User());

Page 19: Fantom and Tales

Less noisy ‘Map’ syntax

String:User map = String:User[:]map[“steve”] = new User();map[“woz”] = new User();

Map<String, User> map = new HashMap<String, User>();map.put(“steve”, new User());map.put(“woz”, new User());

Page 20: Fantom and Tales

Accessor methods

class Person{ String name; Int age { set { … } get{…} }}

Person p = new Person();p.name = “Kaushik”;p.age = 20;

Page 21: Fantom and Tales

default param values

Void printMyLang(Str lang = “java”){ System.out.println(lang);}

printMyLang(); //prints “java”printMyLang(“fantom”); //prints “fantom”

Page 22: Fantom and Tales

THE NOT-SO-LITTLE CHANGES

Page 23: Fantom and Tales

BETTER MODULARIZATION

Page 24: Fantom and Tales

android-1.1.jar guava.jar

com.google.utils.Activity

Which jar?

jscomp.jar tales.jar

Page 25: Fantom and Tales

tales.pod email.pod

tales::Activity

Which pod?

web.pod sys.pod

Page 26: Fantom and Tales

What are we giving up?Unique classes in pod - No two “Utilities” class in a pod

Unique method name in a class - No method overloading

Page 27: Fantom and Tales

easy reflectionclass Article{ public String test(Str name){ …. }}

Obj obj := Article()Str title := Atricle#.method(“test”).invoke(obj)

Page 28: Fantom and Tales

Readable serializationString str = “““acme::Article { title = "Something" viewCount = 123 }”””

Article a = str.in.readObj

Page 29: Fantom and Tales

STATIC AND DYNAMIC TYPING

Page 30: Fantom and Tales

Always compile Never compile

Sometimes compile, Sometimes Don’t

Page 31: Fantom and Tales

Static and dynamicuser := new User()user.doSomething(“”); //Checked by compileruser->doSomething(“”); //Checked by runtime

if dynamic method not present, a trap() implementation is called to do “meta” programming

Page 32: Fantom and Tales

MIXINS

Page 33: Fantom and Tales

interface with implementationmixin Audio{ abstract Int getVolume(); Void incrementVolume() { volume += 1 } Void decrementVolume() { volume -= 1 }}

class Television : Audio{ override Int getVolume(){return 0;}}

Page 34: Fantom and Tales

Pass around methodsprefix := |Str name->Str| { return “Hi $name” }suffix := |Str name->Str| { return “$name, Hello”}

sayWelcome(prefix)sayWelcome(suffix)

Void sayWelcome(|Str->Str| code){ log(“going to say welcome”) code(“Kaushik”) log(“Said welcome”)}

3.times{ echo(“hi”)}

Page 35: Fantom and Tales

ACTORSJust like threads.

but no shared state

Page 36: Fantom and Tales

BETTER APIS

Page 37: Fantom and Tales

DATE AND IOCalendar - Weird c like constants - Months are zero based, Weekdays are 1 based

IO - Over 60 classes - In fantom most functionalities in 4 classes

File, Buf, Instream, OutStream

Page 38: Fantom and Tales

BORING?

Page 39: Fantom and Tales

TALES

Page 40: Fantom and Tales

What’s a framework?

Page 41: Fantom and Tales

Current state of Tales

Page 42: Fantom and Tales

Tales is BoringWithout tales you will need to know atleast

-HTML, css-JavaScript-Fantom-SQL

With tales you will need to know only

-HTML, css-JavaScript-Fantom-SQL

Page 43: Fantom and Tales

Embrace HTML

Page 44: Fantom and Tales

What we will not have// Create a 4 by 4 grid layout.GridLayout grid = new GridLayout(4, 4);grid.addStyleName("example-gridlayout");// Fill out the first row using the cursor.grid.addComponent(new Button("R/C 1"));for (int i = 0; i < 3; i++) { grid.addComponent(new Button("Col " + (grid.getCursorX() + 1)));}// Fill out the first column using coordinates.for (int i = 1; i < 4; i++) { grid.addComponent(new Button("Row " + i), 0, i);}// Add some components of various shapes.grid.addComponent(new Button("3x1 button"), 1, 1, 3, 1);grid.addComponent(new Label("1x2 cell"), 1, 2, 1, 3);InlineDateField date = new InlineDateField("A 2x2 date field");date.setResolution(DateField.RESOLUTION_DAY);grid.addComponent(date, 2, 2, 3, 3);

Page 45: Fantom and Tales

What we will Definitely not have

<%for(int i=0;i<20; i++){%>hello <%=name%>

<%}%>

Page 46: Fantom and Tales

What we will not even have

<c:forEach var="i" begin="1" end="20" step="1" varStatus="status"><c:out value="${i}" />

</c:forEach>

Page 47: Fantom and Tales

What we will have//templateHello <div talesId=”name” />

//fantomadd(label->name(“kaushik”))

//outputHello <div>kaushik</div>

Page 48: Fantom and Tales

What we will have(2)//template<div talesId=”name” />

//fantomUser[] users := User{}.listadd(repeater->name(users)|User user|{ text->value(user.name)})

//output<div>name1</div><div>name2</div><div>name3</div><div>name4</div>

Page 49: Fantom and Tales

EMBRACE JAVASCRIPT

Page 50: Fantom and Tales

What we will not have TextBox box = new TextBox(""); Link l = new Link("click me"); Label a = new Label(""); add(box); add(l); add(a); link.addActionListener(new ActionListener(){

public void actionPerformed(ActionEven e){a.setText(box.getVal());

} });

//because this is easy too..

$("#link").click(function(){$("#label").text($

("#box").val()); });

Page 51: Fantom and Tales

Encapsulate & compileclass Alerter{ Void sayHi(Str? taskName){ client<| alert(taskName); |> }}

..a. sayHi(2); //Will not compile

Page 52: Fantom and Tales

Simpler than DWR Ajaxclass TaskPage : Page{ Void addTask(Str? taskName){ client<| if(!taskName){alert(“No task name”); return;} params[“name”] = taskName; |> server{ taskService.addTask(params[“name”]); } client<|

alert(“Task added”); |> }}

Page 53: Fantom and Tales

Embrace Fantom

Page 54: Fantom and Tales

FIX AND GO

Page 55: Fantom and Tales

Dependency injection & AOPBind{type=UserService#; toType=”UserServiceImpl”},Bind{name = “noOfRows”; toValue=5}

class UserPage{ @inject UserService userService; @inject noOfRows}

Page 56: Fantom and Tales

Embrace HTTP

Page 57: Fantom and Tales

REST

Page 58: Fantom and Tales

Stateful and Scalable

//During a Page Load

//Set values per pagepage[“captcha-val”] = “dixleo”

//During another request from that pageStr captchaVal = page[“captcha-val”]

Page 59: Fantom and Tales

Embrace SQL

Page 60: Fantom and Tales

Part ORM Part Data-mapping//This is validUser u := User{id = 1}.oneUser[] users := User{company = “oracle”}.list

//This is too..User[] users := User{}.queryList(“select id, name from users”)

//you can even externalize your queriesClass BlogSql{ Str userSql := “select id, name from users”}

//And get it injected@inject Sql blogSqlUser[] users := User{}.queryList(blogSql->userSql)