and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics...
Transcript of and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics...
![Page 1: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/1.jpg)
Generics: Past, Present and Future@richardwarburto@raoulUK
![Page 2: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/2.jpg)
binarySearch(List<? extends Comparable<? super T>> list, T key)
![Page 3: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/3.jpg)
Past
Present
Future
![Page 4: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/4.jpg)
![Page 5: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/5.jpg)
… also generics are added to Java.
Yay!
![Page 6: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/6.jpg)
Static Safety Concision
Simplicity
Dynamically Typed Languages - Javascript, Ruby, Python
StringListFantom
GenericsJava, Scala, C#, C++ ...
![Page 7: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/7.jpg)
Past
Present
Future
![Page 8: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/8.jpg)
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
![Page 9: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/9.jpg)
IntersectionA ∩ 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
![Page 10: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/10.jpg)
<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
![Page 11: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/11.jpg)
A Confusing Intersection Type
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
intersection
![Page 12: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/16.jpg)
class Enum<E extends Enum<E>>
![Page 17: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/17.jpg)
Curiously Recurring Generics Pattern
![Page 18: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/18.jpg)
Bounded Wildcards
![Page 19: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/20.jpg)
It’s all about subtyping!
![Page 21: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/21.jpg)
? super
Commonly used for Functional Interfaces
Comparator<Foo>always Comparator<? super Foo>int compare(T o1, T o2);
Comparator<Message> <: Comparator<? super EmailMessage>
Predicate<Foo>always Predicate<? super Foo>boolean test(T t);
Predicate<Message> <: Predicate<? super EmailMessage>
![Page 22: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/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<?>
Chris Parnin, Christian Bird, Emerson Murphy-HillAdoption and use of Java genericshttp://www.cc.gatech.edu/~vector/papers/generics2.pdf
![Page 23: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/23.jpg)
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
![Page 24: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/24.jpg)
Past
Present
Future
![Page 25: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/25.jpg)
Use-site variance
static void logAllWithAction(List<? extends Message> messages,
Consumer<? super Message> action) {
messages.forEach(action);
}
![Page 26: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/26.jpg)
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) {
...
}
![Page 27: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/27.jpg)
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 interfaceshttp://openjdk.java.net/jeps/8043488
![Page 28: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/28.jpg)
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
![Page 29: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/29.jpg)
Type inference and Generics
Map<User, List<Transaction>> userToTransactions
= new HashMap<User, List<Transaction>>();
Since Java 7:
Map<User, List<Transaction>> userToTransactions
= new HashMap<>();
Since Java 8:
BiConsumer<User, Integer> transfer
= (user, amount) -> {};
![Page 30: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/30.jpg)
JEP 286: Local type inference
var userToTransactions = new HashMap<User, List<Transaction>>();
var answer = 42;
var names = Stream.of("Bob", "Marley");
Binary builds available at http://iteratrlearning.com/jep286.html
![Page 31: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/31.jpg)
http://media.community.dell.com/en/dtc/ux9mpc-lbzbuhgas9izg4g41691.png
![Page 32: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/32.jpg)
Poor Sequential Locality (Flatness)
0
1
2
...
User
Name
Id
![Page 33: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/33.jpg)
Value Types
● “codes like a class, works like an int”
● No Identity
● Just a struct of values
![Page 34: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/34.jpg)
Compactness (Less memory)
● No Mark Word○ Locking
● No klass pointer
● Saving 8-16 bytes depending upon architecture/VM
![Page 35: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/35.jpg)
Sequential Locality (Flatness)
0
1
2
...
User
Name
Id
User0
1
2
...
Name
Id
![Page 36: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/36.jpg)
![Page 37: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/37.jpg)
class ArrayList<any T> implements List<T>
![Page 38: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/38.jpg)
List<int> numbers = new ArrayList<>();numbers.add(1);
numbers.add(2);
![Page 39: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/39.jpg)
this.elementData =
new Object[initialCapacity];
![Page 40: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/40.jpg)
null => T.default
![Page 41: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/41.jpg)
What should ArrayList<boolean> store its data in?
![Page 42: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/42.jpg)
You can help
http://cr.openjdk.java.net/~briangoetz/valhalla/specialization.html
http://openjdk.java.net/projects/valhalla/
![Page 43: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/43.jpg)
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)
![Page 44: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/44.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 45: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/45.jpg)
Static Type-safety often involves a tradeoff
between simplicity and flexibility
![Page 46: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/46.jpg)
Any Questions?www.pluralsight.com/author/richard-warburton
www.cambridgecoding.com
www.iteratrlearning.com
http://manning.com/urma http://tinyurl.com/java8lambdas
![Page 47: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/47.jpg)
The EndRichard Warburton(@richardwarburto)
Raoul-Gabriel Urma(@raoulUK)
http://iteratrlearning.com
![Page 48: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/48.jpg)
Mmmm
Java API
<T> List<T>
unmodifiableList(List<? extends T> list)
vs
<T> List<? extends T>
unmodifiableList(List<? extends T> list)
![Page 49: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/49.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 50: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/50.jpg)
![Page 51: and Future Generics: Past, Present @raoulUK @richardwarburtoPast Present Future … also generics are added to Java. Yay! Static Safety Concision Simplicity Dynamically Typed Languages](https://reader034.fdocuments.net/reader034/viewer/2022042311/5ed9e6cda5592118f23433a5/html5/thumbnails/51.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]