48
haXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse [email protected] http://haxe.org

haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse [email protected]

Embed Size (px)

Citation preview

Page 1: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe A Web-oriented

Programming Language

OSCON 2006Nicolas Cannasse

[email protected]://haxe.org

Page 2: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

First Rule

haXe == Simple

Page 3: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

What is haXe ?

It's simple !

Crossplatform : Linux, Windows, OSX, BSD

Multiplatform : JS, Neko, SWF

Easy to get started : let's do it

Page 4: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Hello World

class Hello { static function main() { trace(''Hello World !''); }}

Page 5: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Outputs

Javascript : hello.js

Flash SWF : hello.swf

Neko bytecode : hello.n

Page 6: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Hello World

Let's try it !

Page 7: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Simple

One single language for your whole website...

Simplify the whole development process.

...if you need it.

Page 8: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Simple

One standard crossplatform library

+ Platform-specific libraries :

(root package) : core libraryjs.* / flash.* / neko.* : platform-specific librarieshaxe.* : standard library components, crossplatform

Page 9: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Simple

Created to simplify your life

Anything complicated ?

...fill a bug-report

Simple does not mean featureless

Page 10: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Second Rule

haXe == Powerful

Page 11: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

QUESTION : « What is your favorite programming language »

Page 12: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Dynamicly typed ? This is good

Some people like staticly typed

They have some points too...typos, documentation, refactoring...

But people don't like to write...

Page 13: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

public static HashMap attributesHash( String xmlData ) {try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(xmlData); Element element = doc.getElementById("id"); NamedNodeMap attrs = element.getAttributes(); int numAttrs = attrs.getLength(); HashMap hattribs = new HashMap(); for(int i=0; i<numAttrs; i++) { Attr attr = (Attr)attrs.item(i); String attrName = attr.getNodeName(); String attrValue = attr.getNodeValue(); h.put(attrName,attrValue); } return hattribs;} catch (SAXException e) {} catch (ParserConfigurationException e) {} catch (IOException e) {}return null;

}

Page 14: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

You would prefer instead :function attributesHash( xml ) { var x = Xml.parse(xml); var h = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 15: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Everybody can understand why...

But « what if.... » ?

« Best of both worlds ? »

Page 16: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Let's play a game...

var x = ''Hello'';

var y = x.indexOf("l'');

var z = Math.sqrt(y);

Page 17: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml ) { var x = Xml.parse(xml); var h = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 18: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml : String ) { var x = Xml.parse(xml); var h = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 19: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml : String ) { var x : Xml = Xml.parse(xml); var h = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 20: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml : String ) { var x : Xml = Xml.parse(xml); var h : Hash<???> = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 21: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml : String ) { var x : Xml = Xml.parse(xml); var h : Hash<???> = new Hash(); for( att in x.attributes() ) h.set(att,x.get(att)); return h;}

Page 22: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

Back to the sample :function attributesHash( xml : String ) { var x : Xml = Xml.parse(xml); var h : Hash<???> = new Hash(); for( att : String in x.attributes() ) h.set(att,x.get(att)); return h : Hash<String>;}

Page 23: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

It's called « type inference »

It works with haXe !

One requirement :declare and type you object member variables

Page 24: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Anonymous

Are type-safe :

var o = { name : ''John'', age : 29, city : ''Portland'' };trace(o.name);trace(o.aje); // ERROR

Because :o : { name : String, age : Int, city : String }

Page 25: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Class

Class Sample :

class User {public var name : String;public var age : Int;public function new(n,a) { name = n; age = a; }public function foo() { }

}var u = new User(''John'',29);

Page 26: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Structural

Display Function :function display(o) {

return ''My name is '' + o.name;}

Structural :display(new User(''John'',29));

Page 27: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Functional

An simple function :function f(x,y) {

return ''Sum''+(x + y);}

Type(f) ?var myf : Int -> Int -> String = f;

Page 28: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Duck Typing

class Duck { public function new( ){ } public function say() { trace(''Coin!''); }

}class Dog {

public function new() { } public function say() { trace(''Waf!''); }

}....

var l = new List(); l.add(new Duck()); l.add(new Dog()); for( a in animals ) a.say();

Page 29: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Duck Typing

class Duck { public function new( ){ } public function say() { trace(''Coin!''); }

}class Dog {

public function new() { } public function say() { trace(''Waf!''); }

}....

var l = new List(); l.add(new Duck()); l.add(new Dog()); // ERROR for( a in animals ) a.say();

Page 30: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Duck Typing

class Duck { public function new( ){ } public function say() { trace(''Coin!''); }

}class Dog {

public function new() { } public function say() { trace(''Waf!''); }

}....

var l = new List<{ say : Void -> Void }>(); // WORKS l.add(new Duck()); l.add(new Dog()); for( a in animals ) a.say();

Page 31: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Duck Typing

A small requirement...

Brings you security...

...in the case your Dog doesn't « say » anymore.

Type inference greatly helps refactoring.

Page 32: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Dynamic / untyped

Everything is Dynamic :var o : Dynamic = ''Hello'';

Dynamic is Everything :var v : User = o; // use with care

Untyped : untyped {

.... // don't bother me}

Page 33: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Powerful

haXe is strictly typed

...but you (almost) don't write types

everything is checked... unless you don't want to

the compiler is here to help you, not to bother you

Page 34: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Third Rule

haXe == Extensible

Page 35: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Extensible

« Swiss knife of the web developer »

You can use different tools available...

Like you need

NO Framework : Small Components

Page 36: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Extensible

Crossplatform Libraries ?Conditional compilation :

#if js // js code#else flash // flash code#else neko // neko code#end

Page 37: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

An Example

From Std.hx :public static function parseFloat( x : String ) : Float {

untyped { #if flash return _global["parseFloat"](x); #else neko return __dollar__float(x.__s); #else js return __js__("parseFloat(x)"); #else error #end}

}

Page 38: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Extensible

All the platform-specific code is written in haXe !

... using untyped and other magic

You can redefine everything... if you want

All is transparent

Page 39: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Getting « low-level »

Want to get « low-level » ?

In JS : __js__

In Flash : haXe syntax (plus _global)

In Neko : C libraries / Neko API

Page 40: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

haXe == Extensible

Everything is written in haXe

You can add your own libraries

Even crossplatform

... if you need it !

Page 41: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Fourth Rule

haXe == Ready

Page 42: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Features

Powerful, top-class, compilerfine error reportingno « internal compiler error ». ever.

Great language featurestype inference / polymorphism / duck typingiterators / optional arguments / conditional compilationenums / functional types / type parameters / properties

Page 43: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Libraries

Standard :haxe.Http , serialization , remoting (RPC)template systemunit testing

JS :complete DOM

Neko : mysql, sqlite, regexp, zlib, filesystem, sockets, utf8persistent database objects : SPOD

Page 44: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

NekoVM

Lightweight220K incl. std libary

Embeddablesecure, easy to use API, multithread

Mod_nekofor Apache 1.3.x and Apache 2.2.xallow application caching (save statics)

Page 45: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

NekoVM

Very Fastx86 JITlow memory usage

Computer language Shootoutrecursive(7) : x2.5 over Python , x7 over PHPbintrees(16) : x9 over Python, x120+ over PHP

Open to other languagesRuby on Neko soon ?

Page 46: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Community

http://haxe.org Wikidocumentation, tutorials

Different people, different usagesRIA, Websites, Games

EnthousiasmJust join today and feel free to ask any question

Page 47: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

Conclusion

haXe = { Simple, Powerful, Extensible, Ready,

... and much much more}

Page 48: haXe - Freencannasse.free.fr/files/haxe_oscon2006.pdfhaXe A Web-oriented Programming Language OSCON 2006 Nicolas Cannasse  ncannasse@motion-twin.com

EOF

QUESTIONS ?

Visit http://haxe.org