Generics Past, Present and Future
-
Upload
richardwarburton -
Category
Technology
-
view
438 -
download
0
Transcript of Generics Past, Present and Future
![Page 1: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/1.jpg)
Generics: Past, Present and FutureRichard Warburton@richardwarburto
insightfullogic.com
![Page 2: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/2.jpg)
binarySearch(List<? extends Comparable<? super T>> list, T key)
![Page 3: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/3.jpg)
Past
Present
Future
![Page 4: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/4.jpg)
![Page 5: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/5.jpg)
… also generics are added to Java.
Yay!
![Page 6: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/6.jpg)
Static Safety Concision
Simplicity
Dynamically Typed Languages - Javascript, Ruby, Python
StringListPhantom, Go
GenericsJava, Scala, C#, C++ ...
![Page 7: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/7.jpg)
Past
Present
Future
![Page 8: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/8.jpg)
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
![Page 9: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/9.jpg)
IntersectionA ∩ B = elements has to be a member of both A and B
Intersection Type<T extends A & B> = T has to extend A and B
![Page 10: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/10.jpg)
<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
![Page 11: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/11.jpg)
A Confusing Intersection Type
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
intersection
![Page 12: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/12.jpg)
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
![Page 13: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/13.jpg)
Type erasure
<T extends Comparable<? super T>> T max(Collection<? extends T> coll)
Comparable max(Collection coll)
javac compilation
![Page 14: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/14.jpg)
Type erasure with intersection
<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
Object max(Collection coll)
javac compilation
![Page 15: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/15.jpg)
Serializable lambdas
<T, U extends Comparable<? super U>> Comparator<T>
comparing(Function<? super T, ? extends U> keyExtractor) {
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) ->
keyExtractor.apply(c1)
.compareTo(keyExtractor.apply(c2));
}
![Page 16: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/16.jpg)
class Enum<E extends Enum<E>>
![Page 17: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/17.jpg)
Curiously Recurring Generics Pattern
![Page 18: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/18.jpg)
Bounded Wildcards
![Page 19: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/19.jpg)
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)
![Page 20: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/20.jpg)
It’s all about subtyping!
![Page 21: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/21.jpg)
? super
Commonly used for Functional Interfaces
Comparator<Foo>always Comparator<? super Foo>
Function<Foo, Bar>always Function<? super Foo, ? extends Bar>
![Page 22: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/22.jpg)
Adoption and use of Java generics
90% generics use with Collections○ List<String>, ArrayList<String>,○ HashMap<String,String>, Set<String>
wildcards 10%○ Class<?>
http://www.cc.gatech.edu/~vector/papers/generics2.pdf
![Page 23: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/23.jpg)
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
![Page 24: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/24.jpg)
Past
Present
Future
![Page 25: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/25.jpg)
Declaration-site variance
interface Consumer<? super T> { void accept(T);
}
Improved variance for generic classes and interfaceshttp://openjdk.java.net/jeps/8043488
![Page 26: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/26.jpg)
Primitive specialisation
List<int> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
State of the Specializationhttp://cr.openjdk.java.net/~briangoetz/valhalla/specialization.html
![Page 27: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/27.jpg)
Of interest …
● Unbounded Wildcards
● Type Bounds
● Erasure Problems & Advantages
● Static safety failures
● Other Languages & Features (Lambda Cube)
● Source Code
○ https://github.com/RichardWarburton/generics-examples
![Page 28: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/28.jpg)
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
![Page 29: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/29.jpg)
Backwards compatibility is a benefit we all
receive and a price we all pay.
![Page 30: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/30.jpg)
Any Questions?www.pluralsight.com/author/richard-warburton
cambridgecoding.com
java8training.com
http://manning.com/urma http://tinyurl.com/java8lambdas
![Page 31: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/31.jpg)
The EndRichard Warburton(@richardwarburto)
Raoul-Gabriel Urma(@raoulUK)
![Page 32: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/32.jpg)
Mmmm
Java API
<T> List<T>
unmodifiableList(List<? extends T> list)
vs
<T> List<? extends T>
unmodifiableList(List<? extends T> list)
![Page 33: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/33.jpg)
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
![Page 34: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/34.jpg)
![Page 35: Generics Past, Present and Future](https://reader034.fdocuments.net/reader034/viewer/2022042716/55b4e628bb61ebc2528b45bf/html5/thumbnails/35.jpg)
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]