Sunteți pe pagina 1din 48

Introduction to Ceylon

Stphane pardaud Red Hat

Geecon: An Introduction to Ceylon

Executive summary
What is Ceylon Why Ceylon Features and feel Demo The community Status

Geecon: An Introduction to Ceylon

About Stphane pardaud


Open-Source projects
RESTEasy Ceylon ja!-doclets "lay# modules Stamps$js

Ceylon contri%utor since&


'( )ay *+'' ,one month after Ceylon hit SlashDot compiler ceylondoc .erd

Ri/iera 012 leader http344stephane$epardaud$fr


4

Geecon: An Introduction to Ceylon

Origins o Ceylon
5nitiated and led %y 2a/in 6in7 5mpro/e upon frustrations of 0a/a

Geecon: An Introduction to Ceylon

!hat is Ceylon"
Ceylon is "o8erful reada%le predicta%le Ceylon has 9 platform modularity toolin7

Geecon: An Introduction to Ceylon

Introduction to Ceylon

Geecon: An Introduction to Ceylon

A boring class
:oo;s familiar ri7ht<
class Rectangle() { Integer width = 0; Integer height = 0; Integer area() { return width * height; } }
8

Geecon: An Introduction to Ceylon

A Real Ceylon class


`

=o ,%i7- surprise
shared class Rectangle(width, height) { shared Integer width; shared Integer height; shared Integer area() { return width * height; } }

Geecon: An Introduction to Ceylon

!here is my constructor"
5n the class %ody
shared class Rectangle(width, height) { shared Integer width; shared Integer height; // it is here! if (width == 0 || height == 0) { throw Exception(); } shared Integer area() { return width * height; } }
1 0

Geecon: An Introduction to Ceylon

#irst di erences
Simpler and more re7ular access rules
=o >protected> >pac;a7e> >pri/ate> >shared> ? pu%lic-ish other8ise scope-pri/ate

1 1

Geecon: An Introduction to Ceylon

Attributes
5mmuta%le %y default
class Circle() { Integer scale = 1; variable Integer radius := 2; radius++; Integer diameter { return radius * 2; } assign diameter { radius := diameter / 2; } }

1 2

Geecon: An Introduction to Ceylon

Attributes
1nless mar;ed /aria%le 9ssi7ned 8ith 3? class Circle() { Integer scale = 1; variable Integer radius := 2; radius++; Integer diameter { return radius * 2; } assign diameter { radius := diameter / 2; } }

1 3

Geecon: An Introduction to Ceylon

Attributes
2etter4setter 8ithout carpal tunnel syndrome
class Circle() { Integer scale = 1; variable Integer radius := 2; radius++; Integer diameter { return radius * 2; } assign diameter { radius := diameter / 2; } }

1 4

Geecon: An Introduction to Ceylon

Inheritance
shared class Point(x, y) { shared Integer x; shared Integer y; } shared class Point3D(Integer x, Integer y, z) extends Point(x, y) { shared Integer z; }
1 5

Geecon: An Introduction to Ceylon

Abstractions
)ethod attri%utes and classes can %e o/erridden
Factory pattern

Can@t o/erride %y default


>default>3 can %e o/erridden has a default impl >formal>3 must %e o/erridden 8ith no default impl

>AO/erride> in 0a/a ?B >actual> in Ceylon


=on optional
1 6

Geecon: An Introduction to Ceylon

Abstractions $example%
abstract class Shape() { shared formal Integer area(); // magic: this is toString() shared actual default String string { return "Abstract area: " area.string " m2"; } } class Square(Integer width) extends Shape() { shared actual Integer area() { return width * width; } shared actual String string = "Square area: " area.string " m2"; }
1 7

Geecon: An Introduction to Ceylon

Overloading
=o O/erloadin7
WTF#<

O/erloadin7 is e/il

1 8

Geecon: An Introduction to Ceylon

&ou need overloading'''


To support optional parameters
Ceylon has them E/en named-parameters

To 8or; on different ,su%-types of parameters


=ot safe if a ne8 type is introduced Ceylon has union types and type cases

1 9

Geecon: An Introduction to Ceylon

Optional and named parameters


class Rectangle(Integer width = 2, Integer height = width * 3) { shared Integer area() { return width * height; } } void makeRectangle() { Rectangle rectangle = Rectangle(); Rectangle rectangle2 = Rectangle { height = 4; }; }
2 0

Geecon: An Introduction to Ceylon

(eeping it )R&
interface Figure3D { shared formal Float area; shared formal Float depth; shared formal Float volume; } class Cube(Float width) shared actual Float shared actual Float shared actual Float } satisfies Figure3D { area = width * width; depth = width; volume = area * depth;

class Cylinder(Integer radius, depth) satisfies Figure3D { shared actual Float area = 3.14 * radius ** 2; shared actual Float depth = depth; shared actual Float volume = area * depth; }

2 2

Geecon: An Introduction to Ceylon

Inter aces *ith implementation


interface Figure3D { shared formal Float area; shared formal Float depth; shared Float volume { return area * depth; } } class Cube(Float width) satisfies Figure3D { shared actual Float area = width * width; shared actual Float depth = width; } class Cylinder(Integer radius, Float depth) satisfies Figure3D { shared actual Float area = 3.14 * radius ** 2; shared actual Float depth = depth; }

2 3

Geecon: An Introduction to Ceylon

O+, multiple inheritance mess-"


=o state ,initialiCation=o orderin7 issues 9 sin7le superclass

)ust redefine a method if am%i7uous

2 4

Geecon: An Introduction to Ceylon

Ceylon is extremely regular


Integer attribute = 1; Integer attribute2 { return 2; } void method() {} interface Interface {} class Class(Integer x) { Integer attribute = x; Integer attribute2 { return x; } class InnerClass() {} interface InnerInterface {} void method(Integer y) { Integer attribute; Integer attribute2 { return y; } class LocalClass() {} interface LocalInterface {} void innerMethod() {} } }

Geecon: An Introduction to Ceylon

Hierarchical structure
15
Table table = Table { title = "Squares"; rows = 5; border = Border { padding = 2; weight = 1; }; Column { heading = "x"; width = 10; String content(Integer row) { return row.string; } }, Column { heading = "x**2"; width = 12; String content(Integer row) { return (row**2).string; } } };

2 6

Geecon: An Introduction to Ceylon

#ormal mathematical proo o the type and e ect system

Geecon: An Introduction to Ceylon

Semantics ./.01

2 8

Geecon: An Introduction to Ceylon

2ust (idding-

Geecon: An Introduction to Ceylon

3ypical types
Integer n = 10.times(2); // no primitive types String[] s = {"foo", "bar"}; // inference Number[] r = 1..2; // intervals // inference function makeCube(Float width){ return Cube(width); } value cube2 = makeCube(3.0);

3 0

Geecon: An Introduction to Ceylon

)eath to 45Es

3 1

Geecon: An Introduction to Ceylon

3ype sa ely
// optional? Cube? cubeOrNoCube() { return null; } Cube? cube = cubeOrNoCube(); print(cube.area.string); // compile error if(exists cube) { print(cube.area.string); } else { print("Got no cube"); }
3 2

Geecon: An Introduction to Ceylon

Some sugar on top"


// default value Cube cube = cubeOrNoCube() else Cube(2.0); // nullsafe access Float? area = maybeCube?.area;

3 3

Geecon: An Introduction to Ceylon

Operations on lists
Integer[] numbers = {1,2,3}; // slices Integer[] subList = numbers[1..2]; Integer[] rest = numbers[1...]; // map/spread Integer[] successors = numbers[].successor; Integer[] shifted = numbers[].minus(2);

3 4

Geecon: An Introduction to Ceylon

#unctional programming
// using closures (FP-style) value urls = projectMap.keys .filter(function(String key) key.contains(url)) .map(function(String key) projectMap[key]); // using comprehensions (Imperative-style) value urls2 = { for(key in projectMap.keys) if(key.contains(url)) projectMap[key] };

3 5

Geecon: An Introduction to Ceylon

$some o % 3yping

Geecon: An Introduction to Ceylon

6nion type
To %e a%le to hold /alues amon7 a list of types We must chec; the actual type %efore use >Type9DTypeE> >Type<> is an alias for >TypeD=othin7>

3 7

Geecon: An Introduction to Ceylon

6nion type example


class Apple() { shared void eat() {} } class Garbage() { shared void throwAway() {} } void unions() { Sequence<Apple|Garbage> boxes = {Apple(), Garbage()}; for(Apple|Garbage box in boxes) { print(box.string); if (is Apple box) { box.eat(); } else if (is Garbage box) { box.throwAway(); } } 3 }
8

Geecon: An Introduction to Ceylon

Intersection type
interface Food { shared formal void eat(); } interface Drink { shared formal void drink(); } class Guinness() satisfies Food & Drink { shared actual void drink() {} shared actual void eat() {} } void intersections() { Food & Drink specialStuff = Guinness(); specialStuff.drink(); specialStuff.eat(); }

3 9

Geecon: An Introduction to Ceylon

A lot more eatures


Type parameters Sin7letons and anonymous classes 5ntroductions 9ttri%ute and method references 9ssertions "artial application 9nnotations Type aliases )eta-model 5nterception Tuples

4 0

Geecon: An Introduction to Ceylon

+odularity

Core to the lan7ua7e 5nte7rated in the tool chain


4 1

Geecon: An Introduction to Ceylon

Herd
Our ne!t-7en module repo On http344modules$ceylon-lan7$or7
9lready a/aila%le and usa%le from the tools

5ntuiti/e and 7ood-loo;in7 interface -la 2ithu%


Colla%orati/e

Free Soft8are
"ri/ate repos encoura7ed

Geecon: An Introduction to Ceylon

With some 5DE inside F )ay contain traces of .erd

)emo -

Geecon: An Introduction to Ceylon

Community
Completely open Some from 0Eoss4Red.at 9nd ,/ery- acti/e contri%utors From all o/er 9nd you#

4 4

Geecon: An Introduction to Ceylon

A un pro7ect
=ice people 3 Eest tools
7ithu% ant Eclipse .T):G 98estruct 0a/a 0a/aScript OpenShift "lay#

)any su%projects
spec typechec;er 0H) compiler 0a/aScript compiler Eclipse 5DE We% 5DE SD6 .erd module system ceylondoc 9nt4)a/en plu7ins C:5 plu7ins

Geecon: An Introduction to Ceylon

3he roadmap
Si! milestones to reach '$+ Some features tar7eted to '$'

4 6

Geecon: An Introduction to Ceylon

3o in inity8
)'-)I ,released 9ll 0a/a-style features 9ll the tools ,command-line and 5DE 5nteropera%ility 8ith 0a/a 0a/aScript Enumerated types First-class methods 9nonymous functions File process collection math io net httpd d%c json test SD6 modules )i!in inheritance Comprehensions 9ssertions )em%er classes refinement Type families Type aliases )odule system 5nterop 8ith 0i7sa8 )a/en 2it-style command-line 0H) and 0S %ac;end

)G ,due in t8o 8ee;s Reified 7enerics Tuples Defaulted Type "arameters Typed functions 8ith defaulted parameters
4 7

Geecon: An Introduction to Ceylon

8and beyond )J ,Hersion '$+ alpha9nnotations )etamodel 5nterception SKuash the fe8 remainin7 %u7s

Geecon: An Introduction to Ceylon

Ho* to ind us
Our 8e%site http344ceylon-lan7$or7
Elo7 introduction tour reference spec 9"5 do8nloads .erd3 http344modules$ceylon-lan7$or7

Source repositories
http3447ithu%$com4ceylon

De/elopment4user mailin7 list


2oo7le 7roups3 ceylon-de/ ceylon-users

2oo7leL3 http344ceylon-lan7$or74L T8itter3 Aceylonlan7


4 9

Geecon: An Introduction to Ceylon

9:A
Muestions# 9ns8ers< http344ceylon-lan7$or7

5 0

S-ar putea să vă placă și