47
1 Introduction à Ceylon Stéphane Épardaud Red Hat

Soirée Ceylon avec Stéphane Epardaud

Embed Size (px)

DESCRIPTION

Stéphane Epardaut nous présentera le langage Ceylon : Ceylon est un nouveau langage de programmation pour la machine virtuelle Java. Nous sommes fans de Java et de son écosysteme, cependant nous pensons que le langage Java et ses libraires conçus il y a 15 ans sont désuets face aux challenges actuels. Cette introduction à Ceylon vous montrera les fonctionnalités de Ceylon ainsi que les derniers progrès fait sur le compilateur, l’IDE et la communauté autour de Ceylon.

Citation preview

Page 1: Soirée Ceylon avec Stéphane Epardaud

1

Introduction à CeylonStéphane Épardaud

Red Hat

Page 2: Soirée Ceylon avec Stéphane Epardaud

2

Plan d´attaque

• Ceylon, c'est quoi?

• Pourquoi

• Petit tour

• Démo

• La communauté

• Statut

2

Page 3: Soirée Ceylon avec Stéphane Epardaud

3

À propos de Stéphane Épardaud

• Projets Open-Source–RESTEasy, Ceylon

–jax-doclets, modules Play!, Stamps.js

• Contributeur Ceylon depuis…–13 mai 2011 (un mois après Ceylon sur SlashDot)

–compilo, ceylondoc, Herd

• Équipe Riviera JUG

• http://stephane.epardaud.fr

3

Page 4: Soirée Ceylon avec Stéphane Epardaud

4

Origines de Ceylon

• Lancé et dirigé par Gavin King

• Résoudre les frustrations de Java

4

Page 5: Soirée Ceylon avec Stéphane Epardaud

5

Qu´est-ce que Ceylon ! 

• Ceylon est–puissant, lisible, prédictible

• Ceylon a

–une plate-forme, modularité, outillage

5

Page 6: Soirée Ceylon avec Stéphane Epardaud

6

Introduction à Ceylon

Page 7: Soirée Ceylon avec Stéphane Epardaud

7

Une bête classe

• Ça vous rappelle quelque-chose ?

7

class Rectangle() {    Integer width = 0;    Integer height = 0;        Integer area() {        return width * height;    }}

Page 8: Soirée Ceylon avec Stéphane Epardaud

8

Une vraie classe Ceylon

• Pas (trop) de surprise

8

shared class Rectangle(width, height) {

    shared Integer width;    shared Integer height;        shared Integer area() {        return width * height;    }}

Page 9: Soirée Ceylon avec Stéphane Epardaud

9

Où est mon constructeur ?

• Dans le corps de la classe

9

shared class Rectangle(width, height) {    shared Integer width;    shared Integer height;

    // it is here!    if (width == 0 || height == 0) {

        throw;    }        shared Integer area() {        return width * height;    }}

Page 10: Soirée Ceylon avec Stéphane Epardaud

10

Premières différences

• Règles d´accès plus simple–Pas de `protected`, `package`, `private`

–`shared` = quasi-public, sinon scope-privé

10

Page 11: Soirée Ceylon avec Stéphane Epardaud

11

Attributs

• Immuable par défaut

11

class CircleFromMarseille() {    Integer scale = 1;    variable Integer radius := 2;    radius++;    Integer diameter {        return diameter * 2;    }    assign diameter {        radius := diameter / 2;     }}

Page 12: Soirée Ceylon avec Stéphane Epardaud

12

Attributs

• Sauf si marqué variable

• Affecté avec :=

12

class CircleFromMarseille() {    Integer scale = 1;    variable Integer radius := 2;    radius++;    Integer diameter {        return radius * 2;    }    assign diameter {        radius := diameter / 2;     }}

Page 13: Soirée Ceylon avec Stéphane Epardaud

13

Attributs

• Getter/setter sans le syndrôme du canal carpien

13

class CircleFromMarseille() {    Integer scale = 1;    variable Integer radius := 2;    radius++;    Integer diameter {        return radius * 2;    }    assign diameter {        radius := diameter / 2;     }}

Page 14: Soirée Ceylon avec Stéphane Epardaud

14

Héritage

14

shared class Point(x, y) {    shared Integer x;    shared Integer y;}

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

Page 15: Soirée Ceylon avec Stéphane Epardaud

15

Abstractions

• Méthodes, attributs et classes peuvent être redéfinies–factory pattern

• On ne peut pas redéfinir sauf si–`default`: peut être redéfini, avec impl par défaut

–`formal`: doit être redéfini, sans impl par défaut

• `@Override` en Java => `actual` en Ceylon–Pas optionnel

15

Page 16: Soirée Ceylon avec Stéphane Epardaud

16

Abstractions (exemple)

16

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";}

Page 17: Soirée Ceylon avec Stéphane Epardaud

17

Surcharge

• Pas de surcharge–WTF!?*

• La surcharge, c´est mal

* : Heing !?

17

Page 18: Soirée Ceylon avec Stéphane Epardaud

18

La surcharge, ça sert pour

• Les paramètres optionnels–Ceylon les supporte–Même le passage par nom

• Pour travailler sur des (sous-)types différents–Pas solide si un nouveau sous-type est introduit

–Ceylon supporte les types unions et les switch par type

18

Page 19: Soirée Ceylon avec Stéphane Epardaud

19

Paramètres optionnels Passage par nom

19

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

Page 20: Soirée Ceylon avec Stéphane Epardaud

20

Switch par type

20

void workWithRectangle(Rectangle rect) {}void workWithCircle(Circle circle) {}void workWithShape(Shape shape) {}

shared void supportsSubTyping(Shape fig) {    switch(fig)    case(is Rectangle) {        workWithRectangle(fig);    }    case(is Circle) {        workWithCircle(fig);    }    else {        workWithShape(fig);    }}

Page 21: Soirée Ceylon avec Stéphane Epardaud

21

Non aux répétitions

21

interface Figure3D {    shared formal Float area;    shared formal Float depth;    shared formal Float volume;}

class Cube(Float width) satisfies Figure3D {    shared actual Float area = width * width;    shared actual Float depth = width;    shared actual Float volume = area * depth;}

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

Page 22: Soirée Ceylon avec Stéphane Epardaud

22

Interfaces avec implémentations

22

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, depth)   satisfies Figure3D {    shared actual Float area = 3.14 * radius ** 2;    shared actual Float depth;}

Page 23: Soirée Ceylon avec Stéphane Epardaud

23

Héritage multiple?! Arg…

23

• Pas d´état (initialisation)–Pas de problème de précédence–Une seule superclasse

• Redéfinir méthode en cas d'ambigüité

Page 24: Soirée Ceylon avec Stéphane Epardaud

24

Ceylon est très régulier

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() {}    }}

Page 25: Soirée Ceylon avec Stéphane Epardaud

25

Structure hiérarchique

25

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;        }    }};

Page 26: Soirée Ceylon avec Stéphane Epardaud

26

Preuve mathématique formelle du système de type et d´effet

Page 27: Soirée Ceylon avec Stéphane Epardaud

27

Sémantique 1/154

27

Page 28: Soirée Ceylon avec Stéphane Epardaud

28

STOP!Hammer Time…

Page 29: Soirée Ceylon avec Stéphane Epardaud

29

Types typiques

29

    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);

Page 30: Soirée Ceylon avec Stéphane Epardaud

30

À mort les NPEs

30

Page 31: Soirée Ceylon avec Stéphane Epardaud

31

Typez en sécurité

31

    // 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");    }

Page 32: Soirée Ceylon avec Stéphane Epardaud

32

Un peu de sucre ?

32

    // default value    Cube cube2 = cubeOrNoCube() ? Cube(3.0);    // nullsafe access    Float? area = cube?.area;    // nullsafe array access    Cube[]? maybeList = cubeList();    Cube? c = maybeList?[2];

Page 33: Soirée Ceylon avec Stéphane Epardaud

33

Operations sur les listes

33

Integer[] numbers = {1,2,3};// slicesInteger[] subList = numbers[1..2];Integer[] rest = numbers[1...];// map/spreadInteger[] successors = numbers[].successor;

Page 34: Soirée Ceylon avec Stéphane Epardaud

34

(un peu de) Typage

Page 35: Soirée Ceylon avec Stéphane Epardaud

35

Type union

• Pour stoquer une valeur parmi une liste de types

• `TypeA|TypeB`

• On doit vérifier le vrai type avant de s´en servir

• `Type?` est un alias pour `Type|Nothing`

35

Page 36: Soirée Ceylon avec Stéphane Epardaud

36

Exemple de type union

36

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) {            apple.eat();        } else if (is Garbage box) {            box.throwAway();        }    }}

Page 37: Soirée Ceylon avec Stéphane Epardaud

37

Type intersection

37

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();}

Page 38: Soirée Ceylon avec Stéphane Epardaud

38

Beaucoup d´autres fonctionalités

• Paramètres de types

• Singletons et classes anonymes

• Introductions

• Références d´attribut et de méthode

38

• Clôtures

• Application partielle

• Annotations

• Alias de types

• Meta-modèle

• Interception

Page 39: Soirée Ceylon avec Stéphane Epardaud

39

Modularité

• Intégré au langage

• Et dans les outils39

Page 40: Soirée Ceylon avec Stéphane Epardaud

40

Herd

● Notre dépôt de modules « nouvelle génération »

● Sur http://modules.ceylon-lang.org− Déjà dispo, et utilisable par les

outils● Interface intuitive et jolie à la Github

− Collaboratif● Logiciel Libre

− Dépôts privés encouragés

Page 41: Soirée Ceylon avec Stéphane Epardaud

41

Des mots…et démo !

Avec de vrais morceaux d´IDE

* Peut contenir des traces de Herd

Page 42: Soirée Ceylon avec Stéphane Epardaud

42

Communauté

● Complètement ouverte● Des gens de JBoss/RedHat● Et de (très) actifs contributeurs

− Gary, Andrew, David (Serli), Ross, Tomáš, Sergej, Flavio…

● Et vous !

42

Page 43: Soirée Ceylon avec Stéphane Epardaud

43

Un projet sympa

• Des gens sympas :)• Les meilleurs outils–github, ant, Eclipse, HTML5, Awestruct, Java, JavaScript, OpenShift, Play!

• De nombreux sous-projets–spec, typechecker, Java compiler, JavaScript compiler, Eclipse IDE, Web IDE, Herd, module system, ceylondoc, Ant/Maven plugins

43

Page 44: Soirée Ceylon avec Stéphane Epardaud

44

Vers l´infini…

• Cinq étapes pour atteindre 1.0

• Quelques fonctionalités attendront 1.1

• M1 (finie)–Au minimum toutes les fonctionalités de Java

–Tous les outils (y-compris l´IDE)

• M2 (finie)–Interoperabilité avec Java

–Types énumérés

–Méthodes d´ordre supérieur44

Page 45: Soirée Ceylon avec Stéphane Epardaud

45

…et au delà !

• M3–Fonctions

anonymes

–Modules IO, math–Héritage mixin

• M4–Classes

imbriquées–Alias de types

45

• M5 (Ceylon 1.0)–Annotations

–Génériques réifiés

–Metamodèle–Interception

Page 46: Soirée Ceylon avec Stéphane Epardaud

46

Où nous trouver?

• Notre site : http://ceylon-lang.org–Blog, introduction, tour, référence, spec, API,

téléchargements–Herd : http://modules.ceylon-lang.org

• Dépôt [sic] de source–http://github.com/ceylon

• Listes de développeurs, utilisateurs–Google groups : ceylon-dev, ceylon-users

• Google+ : http://ceylon-lang.org/+• Twitter : @ceylonlang

46

Page 47: Soirée Ceylon avec Stéphane Epardaud

47

Q&R

• Questions ! Réponses ?

• http://ceylon-lang.org

47