View
895
Download
1
Category
Preview:
Citation preview
#DevoxxFR
Java (8) eXperiments
Francois Sarradin @fsarradin
1
#DevoxxFR
François Sarradin
• @ippontech - #IpponOz
• Développeur, architecte, data ingénieur
• Manager technique capitalisation
• brownbaglunch.fr
2
#DevoxxFR
une pratique peut-elle être bonne ?
3
#DevoxxFR
de nouvelles pratiques pour faire face
aux nouveaux besoins
4
#DevoxxFR 5
BECurious
Weird
EXPERIMENTInnovation
#DevoxxFR 6
#DevoxxFR
Sommaire
7
• Héritage multiple
• Exception
• Pattern matching
Et du live coding !
#DevoxxFR
Héritage multiple
8
Java 8
#DevoxxFR
héritage multiple d’état ?
10
#DevoxxFR
Live Demo
11
#DevoxxFR
Ce qu’il faut retenir
12
#DevoxxFR 13
• return in -> 2 * in // => singleton
• return in -> 2 * in + out // => new instance
State multiple inheritance
#DevoxxFR
Exception
15
#DevoxxFR 16
Email is required
John
First name
Last name
Wololo
Age
Age must be an integer
#DevoxxFR
existe-t-il une alternative à throw new Exception(); ?
17
#DevoxxFR
Live Demo
18
#DevoxxFR
Ce qu’il faut retenir
19
#DevoxxFR
Limiter le nombre sous-classes
abstract class Base {
private Base() {}
public static class Class1 extends Base { … }
public static class Class2 extends Base { … }
}
20
#DevoxxFR
Contraindre par le type
21
• Limitation des sous-classes => TDD
• Type = spécification
• ATTENTION !!! null interdit (checkstyle) T = Type
#DevoxxFR
Pattern matching
22
#DevoxxFR
Switch case (Java 7+)
switch (myValue) {
case "hello": System.out.println("hello world"); break;
case "world": System.out.println("world hello"); break;
default : System.out.println("what else!");
}
23
#DevoxxFR
Pattern matching (Scala)
myValue matches {
case "hello" => println("hello world")
case i: Int => println(s"int: $i")
case Add(a, b) => println(s"a + b = ${a + b}")
case _ => println("what else!")
}
24
#DevoxxFR
Live Demo
25
#DevoxxFR
Grammaire
26
MatchCasematch
when
end
Whenthen
Otherwiseotherwise
end
#DevoxxFR
Ce qu’il faut retenir
27
#DevoxxFR
Un DSL en Java qui respecte un grammaire précise…
… C’est possible !
28
#DevoxxFR
Grammaire
29
FirstWord SecondWordfirstWord secondWord
repeat
end
#DevoxxFR
DSL en Java
FirstWord firstWord(T t0) { return new FirstWord(t0); }
class FirstWord { final T t; FirstWord(T t) { this.t = t; }
FirstWord repeat(T tn) { return new FirstWord(t + tn); }
SecondWord secondWord(R r) { return new SecondWord(r); }
class SecondWord { final R r; SecondWord(R r) { this.r = r; }
S end() { return /* … */; } } }
30
#DevoxxFR
Conclusion
31
#DevoxxFR
Ce que nous avons vu
32
Montrer les mécanismes internes à Java 8
Pousser le compilateur à nous aider (contraintes de type)
Spécification analysée par le compilateur
#DevoxxFR
Ce qu’il faut faire
33
Voir d’autres langages (Python, OCaml, Scala)
Voir d’autres approches (FP, logical programming, prototype based programming, acteurs)
Communiquer !
#DevoxxFR
Merci / Thank you
34
Make bugs, not war
Recommended