Your SlideShare is downloading. ×
  • Like
  • Save
DevoxxFR 2013: Lambda are coming. Meanwhile, are you sure we've mastered the generics?
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

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

  • 680 views
Published

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? …

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.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
680
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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. Henri Tremblay OCTO Technology Open Source Responsable R&D EasyMock Performance Objenesis Calcul parallèle
  • 3. September 2013
  • 4. September 2013
  • 5. September 2013
  • 6. What do I need to know? Why© OCTO 2011 7
  • 7. What do I need to know? Why Covariance© OCTO 2011 8
  • 8. What do I need to know? Why Covariance Capture© OCTO 2011 9
  • 9. What do I need to know? Why Covariance Capture Erasure© OCTO 2011 10
  • 10. What do I need to know? Why Covariance Capture Erasure Bridge© OCTO 2011 11
  • 11. What do I need to know? Why Covariance Capture Erasure Bridge© OCTO 2011 12
  • 12. Lambda return Tweet.TWEETS.stream() .collect(Collectors .groupingBy(t->t.containsHashTag("#lambda"), HashMap::new, ArrayList::new));
  • 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. Lambda = Inférence return Tweet.TWEETS.stream() .collect( Collectors .groupingBy(t->t.containsHashTag("#lambda"), HashMap::new, ArrayList::new));
  • 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. Ostrich defense @SuppressWarnings("unchecked")© OCTO 2011 17
  • 17. Why© OCTO 2011 18
  • 18. Rule #1 A code compiling without warning should never ever cause a ClassCastException© OCTO 2011 19
  • 19. Capture© OCTO 2011 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. 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. How the compiler tells the type Determine The parameter the return value type <T> T anyObject(T clazz)© OCTO 2011 23
  • 23. How the compiler tells the type The assigned Determine the variable return type MyType var = <T> T anyObject()© OCTO 2011 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. 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. 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. Example • Collectors.mapping params • Function, Collector • ToIntFunction, Collector.OfInt • ToLongFunction , Collector.OfLong • ToDoubleFunction, , Collector.OfDouble© OCTO 2011 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. 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. 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. Trick #3: Lambda inference Object o = (Runnable) () -> { System.out.println("hi"); }; Collections.sort(strings, (String a, String b) -> a.compareTo(b));
  • 32. Conclusion Who has learned something today?© OCTO 2011 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. Conclusion Questions?© OCTO 2011 35