Generics and Javas Evolution @richardwarburto insightfullogic.com - - PowerPoint PPT Presentation

generics and java s evolution
SMART_READER_LITE
LIVE PREVIEW

Generics and Javas Evolution @richardwarburto insightfullogic.com - - PowerPoint PPT Presentation

Generics and Javas Evolution @richardwarburto insightfullogic.com binarySearch(List<? extends Comparable<? super T>> list, T key) Past Present Future also generics are added to Java. Yay! Simplicity Dynamically Typed


slide-1
SLIDE 1

Generics and Java’s Evolution

@richardwarburto insightfullogic.com

slide-2
SLIDE 2

binarySearch(List<? extends Comparable<? super T>> list, T key)

slide-3
SLIDE 3

Past Present Future

slide-4
SLIDE 4
slide-5
SLIDE 5

… also generics are added to Java. Yay!

slide-6
SLIDE 6

Static Safety Concision Simplicity

Dynamically Typed Languages - Javascript, Ruby, Python StringList Fantom Generics Java, Scala, C#, C++ ...

slide-7
SLIDE 7

Past Present Future

slide-8
SLIDE 8

Intersection Types Curiously Recurring Generics Pattern Wildcards

slide-9
SLIDE 9

Intersection

A ∩ B = elements has to be a member of both A and B

Intersection Type

<T extends A> = T has is a subtype of A <T extends A & B> = T is a subtype of A and B

slide-10
SLIDE 10

<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

slide-11
SLIDE 11

A Confusing Intersection Type

<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

intersection

slide-12
SLIDE 12

Signature pre-generics

public static Object max(Collection coll)

  • max is stuck with this signature to preserve binary

compatibility.

  • Can only find the max if the objects are Comparable
slide-13
SLIDE 13

Type erasure

<T extends Comparable<? super T>> T max(Collection<? extends T> coll) Comparable max(Collection coll)

javac compilation

slide-14
SLIDE 14

Type erasure with intersection

<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) Object max(Collection coll)

javac compilation

slide-15
SLIDE 15

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); } });

slide-16
SLIDE 16

button.addActionListener(event -> System.out.println("button clicked") );

slide-17
SLIDE 17

public interface ActionListener { public void actionPerformed(ActionEvent event); }

slide-18
SLIDE 18

A Comparator Based Upon Keys

<T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor) { return (c1, c2) -> keyExtractor.apply(c1) .compareTo(keyExtractor.apply(c2)); }

slide-19
SLIDE 19

Serializable lambdas

<T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor) { return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1) .compareTo(keyExtractor.apply(c2)); }

slide-20
SLIDE 20

class Enum<E extends Enum<E>>

slide-21
SLIDE 21

Curiously Recurring Generics Pattern

slide-22
SLIDE 22

Bounded Wildcards

slide-23
SLIDE 23

Examples

<T> List<T> unmodifiableList(List<? extends T> list) <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)

slide-24
SLIDE 24

It’s all about subtyping!

slide-25
SLIDE 25

Adoption and use of Java generics

90% generics use with Collections ○ List<String>, ArrayList<String>, ○ HashMap<String,String>, Set<String> wildcards 10% ○ Class<?> Chris Parnin, Christian Bird, Emerson Murphy-Hill Adoption and use of Java generics http://www.cc.gatech.edu/~vector/papers/generics2.pdf

slide-26
SLIDE 26

? super

Commonly used for Functional Interfaces Comparator<Foo>

always Comparator<? super Foo> int compare(T o1, T o2); Comparator<Message> subtypes Comparator<? super EmailMessage>

Predicate<Foo>

always Predicate<? super Foo> boolean test(T t); Predicate<Message> subtypes Predicate<? super EmailMessage>

slide-27
SLIDE 27

Intersection Types Curiously Recurring Generics Pattern Wildcards

slide-28
SLIDE 28

Past Present Future

slide-29
SLIDE 29

Use-site variance

static void logAllWithAction(List<? extends Message> messages, Consumer<? super Message> action) { messages.forEach(action); }

slide-30
SLIDE 30

Declaration-site variance

Library:

interface Consumer< ? super T> { void accept(T t); } interface Iterator< ? extends E> { E next(); ... }

User code:

static void logAllWithAction( Iterator<Message> messages, Consumer<Message> action) { ... }

slide-31
SLIDE 31

Declaration-site variance

  • User-site variance

○ variance complexity pushed to users ○ can add more verbosity due to annotations

  • Declaration-site variance

○ variance complexity pushed to library level ○ List needs to be split in ReadOnly, WriteOnly ○ Adopted by C#, Scala Improved variance for generic classes and interfaces http://openjdk.java.net/jeps/8043488

slide-32
SLIDE 32

Empirical Analysis for Declaration-site variance

  • At least 27% of generic classes and 53% of generic

interfaces in the examined libraries have an inherently variant type parameter.

  • At least 39% of wildcard uses in these libraries could

be made unnecessary with declaration-site variance. John Altidor, Shan Shan Huang, & Yannis Smaragdakis. Taming the Wildcards: Combining Definition- and Use-Site Variance. http://jgaltidor.github.io/variance_pldi11.pdf

slide-33
SLIDE 33

http://media.community.dell.com/en/dtc/ux9mpc-lbzbuhgas9izg4g41691.png

slide-34
SLIDE 34

The Problem

Very Fast Relatively Slow

slide-35
SLIDE 35
  • Luke Gorrie on Mechanical Sympathy

the hardware really wants to run fast and you only need to avoid getting in the way

slide-36
SLIDE 36

Poor Sequential Locality (Flatness)

1 2 ... User Name Id

slide-37
SLIDE 37

Value Types

  • “codes like a class, works like an int”
  • No Identity
  • Just a struct of values
slide-38
SLIDE 38

Sequential Locality (Flatness)

1 2 ... User Name Id User 1 2 ... Name Id

slide-39
SLIDE 39

Compactness (Less memory)

  • No Mark Word

○ Locking

  • No klass pointer
  • Saving 8-16 bytes depending upon architecture/VM
slide-40
SLIDE 40

BUT there’s no identity!

No reference equality No locking No condition variables

slide-41
SLIDE 41
slide-42
SLIDE 42

class ArrayList<any T> implements List<T>

slide-43
SLIDE 43

List<int> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2);

slide-44
SLIDE 44

this.elementData = new Object[initialCapacity];

slide-45
SLIDE 45

null => T.default

slide-46
SLIDE 46

What should ArrayList<boolean> store its data in?

slide-47
SLIDE 47

You can help

http://cr.openjdk.java. net/~briangoetz/valhalla/specialization.html http://openjdk.java.net/projects/valhalla/

slide-48
SLIDE 48

For Reference

  • Source Code

○ https://github.com/RichardWarburton/generics-examples

  • Unbounded Wildcards
  • Type Bounds
  • Erasure Problems & Advantages
  • Static safety failures
  • Other Languages & Features (Lambda Cube)
slide-49
SLIDE 49

Conclusions

  • Usage patterns change as other features are added
  • Generics usage continues to increase in both scale and

complexity

  • Most of the complexity burden is on library authors
slide-50
SLIDE 50

Static Type-safety often involves a tradeoff between simplicity and flexibility

slide-51
SLIDE 51

Any Questions?

www.pluralsight.com/author/richard-warburton www.cambridgecoding.com

www.iteratrlearning.com

http://manning.com/urma http://tinyurl.com/java8lambdas

slide-52
SLIDE 52

The End

Richard Warburton (@richardwarburto)

slide-53
SLIDE 53

Mmmm

Java API <T> List<T> unmodifiableList(List<? extends T> list) vs <T> List<? extends T> unmodifiableList(List<? extends T> list)

slide-54
SLIDE 54

public static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)

From Java 8’s Collectors

slide-55
SLIDE 55
slide-56
SLIDE 56

Higher kinded types

trait Mapable[F[_]] { def map[A, B](fa: F[A])(f: A => B): F[B] } Stream[T] extends Mapable[Stream] Option[T] extends Mapable[Option]