SlideShare a Scribd company logo
Generics: Past, Present
and Future
@richardwarburto
insightfullogic.com
@raoulUK
cambridgecoding.com
binarySearch(List<? extends Comparable<? super T>> list, T key)
Past
Present
Future
… also generics are added to Java.
Yay!
Static Safety Concision
Simplicity
Dynamically Typed
Languages -
Javascript, Ruby,
Python
StringList
Fantom
Generics
Java, Scala, C#, C++
...
Past
Present
Future
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
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
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
A Confusing Intersection Type
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
intersection
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
Type erasure
<T extends Comparable<? super T>>
T max(Collection<? extends T> coll)
Comparable max(Collection coll)
javac compilation
Type erasure with intersection
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll)
Object max(Collection coll)
javac compilation
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));
}
class Enum<E extends Enum<E>>
Curiously Recurring Generics Pattern
Bounded Wildcards
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)
It’s all about subtyping!
? 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>
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
Intersection Types
Curiously Recurring Generics Pattern
Wildcards
Past
Present
Future
Use-site variance
static void logAllWithAction(List<? extends Message> messages,
Consumer<? super Message> action) {
messages.forEach(action);
}
Declaration-site variance
Library:
interface Consumer<? super T> {
void accept(T t);
}
interface StreamStream<? extends T> {
...
}
User code:
static void logAllWithAction(StreamStream<Message> messages,
Consumer<Message> action) {
...
}
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
Value Types
● “codes like a class, works like an int”
● No Identity
● Just a struct of values
Compactness (Less memory)
● No Mark Word
○ Locking
● No klass pointer
● Saving 8-16 bytes depending upon architecture/VM
Sequential Locality (Flatness)
0
1
2
...
User
Name
Id
User
0
1
2
...
Name
Id
BUT there’s yet generics more complexity
● No Identity, reference equality, locking or condition variables
● So we need to add a new concept to generics
● Explicitly need to opt your generic type into any value:
class ArrayList<any T> implements List<T>
Primitive specialisation
List<int> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
State of the Specialization
http://cr.openjdk.java.
net/~briangoetz/valhalla/specialization.html
http://openjdk.java.net/projects/valhalla/
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
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
Static Type-safety often involves a tradeoff
between simplicity and flexibility
Any Questions?
www.pluralsight.com/author/richard-warburton
www.cambridgecoding.com
iteratrlearning.com
http://manning.com/urma http://tinyurl.com/java8lambdas
The End
Richard Warburton
(@richardwarburto)
Raoul-Gabriel Urma
(@raoulUK)
Mmmm
Java API
<T> List<T>
unmodifiableList(List<? extends T> list)
vs
<T> List<? extends T>
unmodifiableList(List<? extends T> list)
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
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]

More Related Content

What's hot

Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
nextlib
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
Gieno Miao
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
Skills Matter
 

What's hot (19)

Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalk
 
Xml processing-by-asfak
Xml processing-by-asfakXml processing-by-asfak
Xml processing-by-asfak
 
Smalltalk, the dynamic language
Smalltalk, the dynamic languageSmalltalk, the dynamic language
Smalltalk, the dynamic language
 
Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
 
Learning core java
Learning core javaLearning core java
Learning core java
 
C#ppt
C#pptC#ppt
C#ppt
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Ruby
RubyRuby
Ruby
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
Java basic data types
Java basic data typesJava basic data types
Java basic data types
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
 
Introduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamicIntroduction to c sharp 4.0 and dynamic
Introduction to c sharp 4.0 and dynamic
 
Dart the better Javascript 2015
Dart the better Javascript 2015Dart the better Javascript 2015
Dart the better Javascript 2015
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
1204csharp
1204csharp1204csharp
1204csharp
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Java basic datatypes
Java basic datatypesJava basic datatypes
Java basic datatypes
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Primitives in Generics
Primitives in GenericsPrimitives in Generics
Primitives in Generics
 

Similar to Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton

Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
oscon2007
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
oscon2007
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrong
Julien Wetterwald
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
g_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
Sachin Singh
 

Similar to Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton (20)

Generics past, present and future
Generics  past, present and futureGenerics  past, present and future
Generics past, present and future
 
Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)Generics Past, Present and Future (Latest)
Generics Past, Present and Future (Latest)
 
Generics Past, Present and Future
Generics Past, Present and FutureGenerics Past, Present and Future
Generics Past, Present and Future
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrong
 
Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
C, C++ Training Institute in Chennai , Adyar
C, C++ Training Institute in Chennai , AdyarC, C++ Training Institute in Chennai , Adyar
C, C++ Training Institute in Chennai , Adyar
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
Scala Days NYC 2016
Scala Days NYC 2016Scala Days NYC 2016
Scala Days NYC 2016
 
Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Javatraining
JavatrainingJavatraining
Javatraining
 
Cpprm
CpprmCpprm
Cpprm
 
Testing for share
Testing for share Testing for share
Testing for share
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 

More from JAXLondon_Conference

All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...
JAXLondon_Conference
 
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo EuteneuerStop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
JAXLondon_Conference
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
JAXLondon_Conference
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
JAXLondon_Conference
 
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay PrewerSmoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
JAXLondon_Conference
 
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos ErotocritouHybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
JAXLondon_Conference
 

More from JAXLondon_Conference (20)

Cassandra and Spark - Tim Berglund
Cassandra and Spark - Tim BerglundCassandra and Spark - Tim Berglund
Cassandra and Spark - Tim Berglund
 
All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...All change! How the new Economics of Cloud will make you think differently ab...
All change! How the new Economics of Cloud will make you think differently ab...
 
The Unit Test is dead. Long live the Unit Test! - Colin Vipurs
The Unit Test is dead. Long live the Unit Test! - Colin VipursThe Unit Test is dead. Long live the Unit Test! - Colin Vipurs
The Unit Test is dead. Long live the Unit Test! - Colin Vipurs
 
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo EuteneuerStop guessing, start testing – mobile testing done right - Timo Euteneuer
Stop guessing, start testing – mobile testing done right - Timo Euteneuer
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
 
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel UrmaJava Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
Java Generics Past, Present and Future - Richard Warburton, Raoul-Gabriel Urma
 
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay PrewerSmoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
Smoothing the continuous delivery path – a tale of two teams - Lyndsay Prewer
 
VC from the inside - a techie's perspective - Adrian Colyer
VC from the inside - a techie's perspective - Adrian ColyerVC from the inside - a techie's perspective - Adrian Colyer
VC from the inside - a techie's perspective - Adrian Colyer
 
Use your type system; write less code - Samir Talwar
Use your type system; write less code - Samir TalwarUse your type system; write less code - Samir Talwar
Use your type system; write less code - Samir Talwar
 
Thinking fast and slow with software development - Daniel Bryant
Thinking fast and slow with software development - Daniel BryantThinking fast and slow with software development - Daniel Bryant
Thinking fast and slow with software development - Daniel Bryant
 
The java memory model and the mutability matrix of pain - Jamie Allen
The java memory model and the mutability matrix of pain - Jamie AllenThe java memory model and the mutability matrix of pain - Jamie Allen
The java memory model and the mutability matrix of pain - Jamie Allen
 
The art of shifting perspectives - Rachel Davies
The art of shifting perspectives - Rachel DaviesThe art of shifting perspectives - Rachel Davies
The art of shifting perspectives - Rachel Davies
 
Spring Boot in the Web Tier - Dave Syer
Spring Boot in the Web Tier - Dave SyerSpring Boot in the Web Tier - Dave Syer
Spring Boot in the Web Tier - Dave Syer
 
Microservices from dream to reality in an hour - Dr. Holly Cummins
Microservices from dream to reality in an hour - Dr. Holly CumminsMicroservices from dream to reality in an hour - Dr. Holly Cummins
Microservices from dream to reality in an hour - Dr. Holly Cummins
 
Love your architecture - Alexander von Zitzewitz
Love your architecture - Alexander von ZitzewitzLove your architecture - Alexander von Zitzewitz
Love your architecture - Alexander von Zitzewitz
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Java vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris BaileyJava vs. Java Script for enterprise web applications - Chris Bailey
Java vs. Java Script for enterprise web applications - Chris Bailey
 
Java 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen ColebourneJava 8 best practices - Stephen Colebourne
Java 8 best practices - Stephen Colebourne
 
Intuitions for scaling data centric architectures - Benjamin Stopford
Intuitions for scaling data centric architectures - Benjamin StopfordIntuitions for scaling data centric architectures - Benjamin Stopford
Intuitions for scaling data centric architectures - Benjamin Stopford
 
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos ErotocritouHybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
Hybrid solutions – combining in memory solutions with SSD - Christos Erotocritou
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
Max Lee
 

Recently uploaded (20)

Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring Software
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
How To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdfHow To Build a Successful SaaS Design.pdf
How To Build a Successful SaaS Design.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
 

Java generics past, present and future - Raoul-Gabriel Urma, Richard Warburton