Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

DevoxxFR 2013: Lambda are coming. Meanwhile, are you sure we've mastered the generics?

1,077 views

Published on

Lambda expressions are coming soon. They promise to be a nice complexity cocktail with the 9 years old generics. So, before confronting the lambdas, are you sure you're mastered the generics?

This quick talk will give you some tips to learn to swim before the flood.

  • Be the first to comment

  • Be the first to like this

DevoxxFR 2013: Lambda are coming. Meanwhile, are you sure we've mastered the generics?

  1. 1. Les lambda arrivent!En attendant, êtes-vous sûr davoir compris les génériques? Henri Tremblay Architecte Senior OCTO Technology @henri_tremblay 27 au 29 mars 2013
  2. 2. Henri Tremblay OCTO Technology Open Source Responsable R&D EasyMock Performance Objenesis Calcul parallèle
  3. 3. September 2013
  4. 4. September 2013
  5. 5. September 2013
  6. 6. What do I need to know? Why© OCTO 2011 7
  7. 7. What do I need to know? Why Covariance© OCTO 2011 8
  8. 8. What do I need to know? Why Covariance Capture© OCTO 2011 9
  9. 9. What do I need to know? Why Covariance Capture Erasure© OCTO 2011 10
  10. 10. What do I need to know? Why Covariance Capture Erasure Bridge© OCTO 2011 11
  11. 11. What do I need to know? Why Covariance Capture Erasure Bridge© OCTO 2011 12
  12. 12. Lambda return Tweet.TWEETS.stream() .collect(Collectors .groupingBy(t->t.containsHashTag("#lambda"), HashMap::new, ArrayList::new));
  13. 13. Lambda = Fun with generics Stream<Tweet> stream = Tweet.TWEETS.stream(); Function<Tweet, Boolean> lambda = t -> t.containsHashTag("#lambda"); Collector<Tweet, Map<Boolean, List<Tweet>>> collector = Collectors.<Tweet, Boolean, List<Tweet>, Map<Boolean, List<Tweet>>> groupingBy(lambda, HashMap::new, ArrayList::new); return stream.collect(collector);
  14. 14. Lambda = Inférence return Tweet.TWEETS.stream() .collect( Collectors .groupingBy(t->t.containsHashTag("#lambda"), HashMap::new, ArrayList::new));
  15. 15. Dreaded warnings Type safety: The expression of type List needs unchecked conversion to conform to List<String> Type safety: Unchecked cast from List<capture#1-of ?> to List<String>© OCTO 2011 16
  16. 16. Ostrich defense @SuppressWarnings("unchecked")© OCTO 2011 17
  17. 17. Why© OCTO 2011 18
  18. 18. Rule #1 A code compiling without warning should never ever cause a ClassCastException© OCTO 2011 19
  19. 19. Capture© OCTO 2011 20
  20. 20. Capture usually is Type List<?> bar(); <T> IExpectationSetters<T> expect(T value); void andReturn(T value); // Method of IExpectationSetters Returns a capture#6 expect(bar()).andReturn(new ArrayList<String>()); And you get The method andReturn(List<capture#6-of ?>) in the type IExpectationSetters<List<capture#6-of ?>> is not applicable for the arguments (ArrayList<String>)© OCTO 2011 21
  21. 21. Only solution We need to cast expect((List<String>) bar()).andReturn(new ArrayList<String>()); Tell to expect we want a List<String> But still a warning Type safety: Unchecked cast from List<capture#6-of ?> to List<String> Framework coder tip: Try to never return a wildcard unless necessary© OCTO 2011 22
  22. 22. How the compiler tells the type Determine The parameter the return value type <T> T anyObject(T clazz)© OCTO 2011 23
  23. 23. How the compiler tells the type The assigned Determine the variable return type MyType var = <T> T anyObject()© OCTO 2011 24
  24. 24. But watch out with overloading public void foo(String s) public void foo(Object o) foo(anyObject()); Can’t guess the type© OCTO 2011 25
  25. 25. Trick #1 Determine the return Artificially give the type with a value type dedicated parameter <T> T anyObject(Class<T> clazz)© OCTO 2011 26
  26. 26. But how to pass a generic? public void foo(String s) public void foo(Object o) foo(anyObject(List<String>.class)); Examples: Illegal ToXXXFunction© OCTO 2011 27
  27. 27. Example • Collectors.mapping params • Function, Collector • ToIntFunction, Collector.OfInt • ToLongFunction , Collector.OfLong • ToDoubleFunction, , Collector.OfDouble© OCTO 2011 28
  28. 28. Some solutions This would work foo((String) anyObject()); foo((List<String>) anyObject()); // Warning But still doesn’t work for generics© OCTO 2011 29
  29. 29. Trick #2 So the only solution is foo(EasyMock.<List<String>> anyObject()); … which sadly doesn’t support static imports foo(.<List<String>> anyObject()); // Illegal© OCTO 2011 30
  30. 30. Trick #2 applied to Lambda Return type: Map<Boolean, List<Tweet>> return Tweet.TWEETS.stream() .collect( Collectors.<Tweet, Boolean, List<Tweet>, Map<Boolean, List<Tweet>>> groupingBy(t->t.containsHashTag("#lambda"), HashMap::new, ArrayList::new));
  31. 31. Trick #3: Lambda inference Object o = (Runnable) () -> { System.out.println("hi"); }; Collections.sort(strings, (String a, String b) -> a.compareTo(b));
  32. 32. Conclusion Who has learned something today?© OCTO 2011 33
  33. 33. Useful links Nice lambda tutorial (in French): http://lambda.ninjackaton.ninja-squad.com/ Description of inference types on lambda: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html Everything on generics: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
  34. 34. Conclusion Questions?© OCTO 2011 35

×