SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
Le pourquoi du comment
du type Optional.
Fabrice Sznajderman
Paris JUG - 2015
Qui vous parle?
Fabrice Sznajderman
Développeur Java / Scala / Web
Twitter : @fsznajderman
Github : fabszn
Paris JUG - 2015
Paris JUG - 2015
/**
* I promise the following service will never return a NULL reference
*/
Contact c = cs.getContactById(1);
System.out.println(“Age =” + c.getAge());
Paris JUG - 2015
NullPointerException
/**
* I promise the following service will never return a NULL reference
*/
Contact c = cs.getContactById(1);
System.out.println(“Age =” + c.getAge());
...
Paris JUG - 2015
/**
* I promise the following service will never return a NULL reference
*/
Contact c = cs.getContactById(1);
if (c != null) {
System.out.println(“Age =” + c.getAge());
}
...
Paris JUG - 2015
/**
* I promise the following service will never return a NULL reference
*/
Contact c = cs.getContactById(1);
if (c != null) {
System.out.println(“Age =” + c.getAge());
}
...
Type Optional: Un peu de théorie...
‘Optional est un type permettant de caractériser la
présence ou l’absence de valeur’
Paris JUG - 2015
contact
présence absence
Type Optional: Un peu de théorie...
Contact c1 = null;
Contact c2 = new Contact("Hari","kovair",34);
Optional<Contact> o1 = Optional.ofNullable(c1);
Optional<Contact> o2 = Optional.of(c2);
System.out.println("o1 = " + o1);
System.out.println("o2 = " + o2);
Paris JUG - 2015
Type Optional: Un peu de théorie...
Contact c1 = null;
Contact c2 = new Contact("Hari","kovair",34);
Optional<Contact> o1 = Optional.ofNullable(c1);
Optional<Contact> o2 = Optional.of(c2);
System.out.println("o1 = " + o1);
System.out.println("o2 = " + o2);
Paris JUG - 2015
o1 = Optional.empty
o2 = Optional[model.Contact@54bedef2]
Type Optional: Un peu de théorie...
‘Une monade est comme une boite, vide ou ayant
un contenu, qui nous fournit des opérations au
dessus de la valeur éventuellement encapsulée.’
Paris JUG - 2015
contact ou
.map()
.orElse()
.get()
.filter()
...
Optional
Type Optional : Cas pratiques
Live coding...
Paris JUG - 2015
Type Optional : Conclusion
● C’est bon pour l’environnement!
● Disparition du pattern if-null
● Documentation automatique
● Disponible dans d’autres langages
Paris JUG - 2015
final Optional<Questions> qs = getQuestions();
qs.map( q → ”Answers : ” + q.answers ).orElse(“Merci de votre attention!”)
Paris JUG - 2015

Weitere ähnliche Inhalte

Andere mochten auch

La gestion des droits numériques - CNAM
La gestion des droits numériques - CNAMLa gestion des droits numériques - CNAM
La gestion des droits numériques - CNAMFabrice Sznajderman
 
Maven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieuxMaven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieuxFabrice Sznajderman
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
 
Scala in Action - Heiko Seeburger
Scala in Action - Heiko SeeburgerScala in Action - Heiko Seeburger
Scala in Action - Heiko SeeburgerJAX London
 
Paris stormusergroup intrudocution
Paris stormusergroup intrudocutionParis stormusergroup intrudocution
Paris stormusergroup intrudocutionParis_Storm_UG
 
Getting Functional with Scala
Getting Functional with ScalaGetting Functional with Scala
Getting Functional with ScalaJorge Paez
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with ScalaHimanshu Gupta
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable LanguageMario Gleichmann
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Mémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociauxMémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociauxChloé Marty
 
Lagom, reactive framework(chtijug2016)
Lagom, reactive framework(chtijug2016) Lagom, reactive framework(chtijug2016)
Lagom, reactive framework(chtijug2016) Fabrice Sznajderman
 
Démystifions le machine learning avec spark par David Martin pour le Salon B...
Démystifions le machine learning avec spark par David Martin pour le Salon B...Démystifions le machine learning avec spark par David Martin pour le Salon B...
Démystifions le machine learning avec spark par David Martin pour le Salon B...Ippon
 
BDX 2016 - Tzach zohar @ kenshoo
BDX 2016 - Tzach zohar  @ kenshooBDX 2016 - Tzach zohar  @ kenshoo
BDX 2016 - Tzach zohar @ kenshooIdo Shilon
 

Andere mochten auch (20)

La gestion des droits numériques - CNAM
La gestion des droits numériques - CNAMLa gestion des droits numériques - CNAM
La gestion des droits numériques - CNAM
 
Maven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieuxMaven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieux
 
Universitélang scala tools
Universitélang scala toolsUniversitélang scala tools
Universitélang scala tools
 
Les monades Scala, Java 8
Les monades Scala, Java 8Les monades Scala, Java 8
Les monades Scala, Java 8
 
Université des langages scala
Université des langages   scalaUniversité des langages   scala
Université des langages scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Lagom, reactive framework
Lagom, reactive frameworkLagom, reactive framework
Lagom, reactive framework
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Scala in Action - Heiko Seeburger
Scala in Action - Heiko SeeburgerScala in Action - Heiko Seeburger
Scala in Action - Heiko Seeburger
 
Paris stormusergroup intrudocution
Paris stormusergroup intrudocutionParis stormusergroup intrudocution
Paris stormusergroup intrudocution
 
Getting Functional with Scala
Getting Functional with ScalaGetting Functional with Scala
Getting Functional with Scala
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with Scala
 
Soutenance ysance
Soutenance ysanceSoutenance ysance
Soutenance ysance
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable Language
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Mémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociauxMémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociaux
 
Lagom, reactive framework(chtijug2016)
Lagom, reactive framework(chtijug2016) Lagom, reactive framework(chtijug2016)
Lagom, reactive framework(chtijug2016)
 
Démystifions le machine learning avec spark par David Martin pour le Salon B...
Démystifions le machine learning avec spark par David Martin pour le Salon B...Démystifions le machine learning avec spark par David Martin pour le Salon B...
Démystifions le machine learning avec spark par David Martin pour le Salon B...
 
BDX 2016 - Tzach zohar @ kenshoo
BDX 2016 - Tzach zohar  @ kenshooBDX 2016 - Tzach zohar  @ kenshoo
BDX 2016 - Tzach zohar @ kenshoo
 

Paris jug option

  • 1. Le pourquoi du comment du type Optional. Fabrice Sznajderman Paris JUG - 2015
  • 2. Qui vous parle? Fabrice Sznajderman Développeur Java / Scala / Web Twitter : @fsznajderman Github : fabszn Paris JUG - 2015
  • 3. Paris JUG - 2015 /** * I promise the following service will never return a NULL reference */ Contact c = cs.getContactById(1); System.out.println(“Age =” + c.getAge());
  • 4. Paris JUG - 2015 NullPointerException /** * I promise the following service will never return a NULL reference */ Contact c = cs.getContactById(1); System.out.println(“Age =” + c.getAge()); ...
  • 5. Paris JUG - 2015 /** * I promise the following service will never return a NULL reference */ Contact c = cs.getContactById(1); if (c != null) { System.out.println(“Age =” + c.getAge()); } ...
  • 6. Paris JUG - 2015 /** * I promise the following service will never return a NULL reference */ Contact c = cs.getContactById(1); if (c != null) { System.out.println(“Age =” + c.getAge()); } ...
  • 7. Type Optional: Un peu de théorie... ‘Optional est un type permettant de caractériser la présence ou l’absence de valeur’ Paris JUG - 2015 contact présence absence
  • 8. Type Optional: Un peu de théorie... Contact c1 = null; Contact c2 = new Contact("Hari","kovair",34); Optional<Contact> o1 = Optional.ofNullable(c1); Optional<Contact> o2 = Optional.of(c2); System.out.println("o1 = " + o1); System.out.println("o2 = " + o2); Paris JUG - 2015
  • 9. Type Optional: Un peu de théorie... Contact c1 = null; Contact c2 = new Contact("Hari","kovair",34); Optional<Contact> o1 = Optional.ofNullable(c1); Optional<Contact> o2 = Optional.of(c2); System.out.println("o1 = " + o1); System.out.println("o2 = " + o2); Paris JUG - 2015 o1 = Optional.empty o2 = Optional[model.Contact@54bedef2]
  • 10. Type Optional: Un peu de théorie... ‘Une monade est comme une boite, vide ou ayant un contenu, qui nous fournit des opérations au dessus de la valeur éventuellement encapsulée.’ Paris JUG - 2015 contact ou .map() .orElse() .get() .filter() ... Optional
  • 11. Type Optional : Cas pratiques Live coding... Paris JUG - 2015
  • 12. Type Optional : Conclusion ● C’est bon pour l’environnement! ● Disparition du pattern if-null ● Documentation automatique ● Disponible dans d’autres langages Paris JUG - 2015
  • 13. final Optional<Questions> qs = getQuestions(); qs.map( q → ”Answers : ” + q.answers ).orElse(“Merci de votre attention!”) Paris JUG - 2015