SlideShare una empresa de Scribd logo
1 de 47
Flexible
Types
in
Kotlin
Andrey Breslav
JetBrains
* http://www.v3.co.uk/IMG/686/292686/flexible-working-1.jpg
http://kotlinlang.org
* http://www.v3.co.uk/IMG/686/292686/flexible-working-1.jpg* http://resizing.flixster.com/X2ini4ixLj_y12UNKHZrkZVKm80=/800x1200/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/94/11189423_ori.jpg
Is the glass full?
* http://examinetheglass.com/wp-content/uploads/2011/08/glass-of-water1.jpg
* http://glsindia.com/wp-content/uploads/2013/08/team_fortress_2__the_engineer_by_onosaka_yuha-d47vyqv1.jpg
Full enough
Practically,
it’s full
+
* http://www.howtovanish.com/wp-content/uploads/2011/01/Safe.jpg
Safety Features
 Nullable Types
 Read-Only Collections
 Invariant Arrays
 Raw Types
+
Interoperability
 Kotlin can call Java
 No overhead in code
 No runtime overhead
 No preventing intended uses of
Java APIs
* http://staffingstream.wpengine.netdna-cdn.com/wp-content/uploads/2013/01/integration.jpg
+
Example: Arrays
// Java
class Util {
public static int countNonNull(Object[] arr) {…}
}
// Kotlin
val strings = arrayOf(“a”, “b”)
Util.countNonNull(strings)
+
* http://staffingstream.wpengine.netdna-cdn.com/wp-content/uploads/2013/01/integration.jpg
Interoperability
 Kotlin can call Java
 No overhead in code
 No runtime overhead
 No preventing intended uses of
Java APIs
 Pure Kotlin’s safety should not be
compromised
+
The Ultimate Disappointment
Nulls
* http://www.maciejratajski.com/sites/default/files/work/image/ratajski-knowledge-1.jpg
Java String s = null;
s.length();
Errors At Runtime
Kotlin val s: String
s.length()
val s: String? = null
s.length()
Errors At Compile Time
= null
Nullable
type
Check and use val s: String? = …
if (s != null) {
s.length()
}
Check and exit if (s == null) return
s.length()
Rock’n’Roll s?.length()
s!!.length()
(s ?: “…”).length()
Kotlin is good with nulls
What about
Java?
+
Java (as seen from Kotlin)
public class JavaClass {
public String foo(List<String> l) {…}
}
String
String?
List<String>
List<String?>
List<String>?
List<String?>?
Safest!
+
Java Interop: All Nullable
javaValue.toString().length() + 1
javaValue?.toString()?.length()!! + 1
val l: List<String> = javaValue?.getList()!!
ArrayList<String?>
Your safest
option doesn’t
work!
+
Annotations
public class JavaClass {
@NotNull
public String foo(@NotNull List<String> l) {…}
}
String
String?
List<String>
List<String?>
List<String>?
List<String?>?
Annotations are
cumbersome
AND
don’t really help!
+
Pick Two
 Null-safety
 Convenience
 Java Interop
Flexible Types!
Thanks to
Dr. Ross Tate of
+
Java: Flexible Types
public class JavaClass {
public String foo(Bar<String> l) {…}
}
String! Bar<String!>!
Flexible Type
+
Dereferencing Flexible Values
s: String s: String? s: String!
s.length() s.length() s.length()
s?.length() s?.length() s?.length()
s!!.length() s!!.length() s!!.length()
NPE
+
Assignability
String String?
String!
Flexible Type
NPE
+
Representation
String! =
String?
String
String =
String
String
Optimistic Subtyping:
picks the most convenient end
String?
String
<:
String
String
String?
String?
<:
+
ArrayList<String!> <: ArrayList<String>
“Equivalence”
A ≈ B  A <: B & B <: A
String?
String
≈
String
String
String?
String?
≈
“equivalent”
• Reflexive
• Symmetric
• NOT Transitive
+
Runtime Checks
(and their absence)
 String <- String!
 Fails if the value is null
 List<String> <- ArrayList<String!>!
 Fails if the list is null
 Passes if an element is null
* http://img06.deviantart.net/1744/i/2013/082/5/b/there_s_a_hole_in_the_fence_where_i_used_to_see__by_tricyclemishap-d5z1u3w.jpg
+
Some Notes
 Flexible Types are Not Denotable!
 String! is notation, not syntax
 Pure Kotlin is Null-Safe
 Kotlin+Java is as safe as Java
 Annotations Still Applicable
 @NotNull String in Java becomes String in Kotlin
+
Arrays
* http://www.performing-musician.com/pm/feb09/images/TechNotes_03_WideLine_array.jpg
There are many Java APIs that talk about arrays,
which will never change
-- John Rose
+
Kotlin Arrays
 Array<T> is invariant
 Array<String> >:< Array<Any>
 Array<out T> is a covariant projection
 similar to Array<? extends T>
 Array<out T> <: Array<Any>
 Can’t call arr.set(x)
Array<out Foo!>?
Array<Foo!>
Foo[] =>
+
Arrays: Example
Array<out Any>?
Array<Any>
Array<String>
Array<String>
<:
JDK Collections interface List<E> {
E get(int index);
E set(int index, E value);
}
Read-only /** read-only */
List<Foo> getFoos() {
return unmodifiableList(l);
}
Errors At Runtime
Cool Collections interface List<E> {
E get(int index);
}
interface MutableList<E>
extends List<E> {
E set(int index, E value);
}
Read-only List<Foo> getFoos() {
return l; // may be mutable
}
Errors At Compile Time
Iterable<out T>
Collection<out T>
List<out T> Set<out T>
MutableIterable<T>
MutableCollection<T>
MutableList<T> MutableSet<T>
java.lang.ArrayList<T> java.util.HashSet<T>
Kotlin
Collections
JDK
Collections
+
Java: Flexible Types
public class JavaClass {
public String foo(Bar<String> l) {…}
}
Bar<String!>!
Flexible Type
+
Java: Flexible Types
public class JavaClass {
public String foo(List<String> l) {…}
}
(Mutable?)List<String!>!
List<String!>?
MutableList<String!>
Kotlin Collections
Safe
Compatible
Co-variant
When
there’s
no Java!
+
And Then It Gets Bad
public class JavaClass {
public String foo(List<Object> l) {…}
}
List<Any!>?
MutableList<Any!>
List<String>
List<String>
<:
* http://www.psdgraphics.com/file/cracked-floor-background.jpg
val strs = listOf(“abc”, “def”)
JavaClass.foo(strs) // 
+
It’s Inevitable 
val strs = listOf(“abc”, “def”)
val anys: List<Any> = strs
JavaClass.foo(anys) // 
* http://www.psdgraphics.com/file/cracked-floor-background.jpg
 kotlin.List is covariant
 kotlin.List is not final
 We enable intended use of normal APIs
 kotlin.List<Any> is assignable to java.util.List<Object>
+
Not Even
As Safe As Java
* http://huntonit.com/media/12233/Huntonit-Design-Hole-in-wal.jpg
Safe enough
+
Ad Hoc Check
(that cures nothing, but helps a lot)
 Whenever we see a call javaMethod(kotlinCollection)
 After normal subtype checks
 Perform an extra check
* http://digitalsophisticate.com/wp-content/uploads/2013/09/Duct-Tape-2.jpg
val strs = listOf(“abc”, …)
JavaClass.foo(strs) // 
val anys: List<Any> = strs
JavaClass.foo(anys) // 
+
Runtime Checks
(and their absence)
 MutableList <- (Mutable)List
 Fails if the value is not mutable
 No checks for the list contents
 Where “mutable” means
 All Java collection implementations
 including unmodifiable*() 
 Kotlin classes that extend Mutable*
* http://img06.deviantart.net/1744/i/2013/082/5/b/there_s_a_hole_in_the_fence_where_i_used_to_see__by_tricyclemishap-d5z1u3w.jpg
* http://i.ytimg.com/vi/K92S_n79ETk/maxresdefault.jpg
+
Raw Types
 Still occur in real world
 More often than I’d like
 class Rec<T extends Rec> { … }
 Important for binding overrides
 Can’t assign raw Foo to Foo<T>, but it’s OK
 Explicit (uncheced) cast required
Foo<out Bar!>?
Foo<Bar!>
raw Foo<T extends Bar> =>
+
Summary (I)
 Nullable types
 Same as in Java
 Enhanced by (optional) annotations
 Some runtime checks
 Arrays
 Same as in Java
 Collections
 Worse than Java
 Hacky ad hoc check (not a cure, but it helps)
 Enhanced by (optional) annotations
 Some runtime checks
 Raw types
 Usable, but not as smooth as in Java
+
Summary (II)
More safety than Java
+
Seamless interop
=
“gradual” types that are unsound
(i.e. less safety, but not so often) Safe enough
http://kotlinlang.org
+
Expressing dynamic types
(for JS)
Any?
Nothing
dynamic =>

Más contenido relacionado

Destacado (10)

JVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in KotlinJVMLS 2016. Coroutines in Kotlin
JVMLS 2016. Coroutines in Kotlin
 
Kotlin for Android: Brief and Clear
Kotlin for Android: Brief and ClearKotlin for Android: Brief and Clear
Kotlin for Android: Brief and Clear
 
Functions and data
Functions and dataFunctions and data
Functions and data
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Kotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language designKotlin: Challenges in JVM language design
Kotlin: Challenges in JVM language design
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Introduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clearIntroduction to Kotlin: Brief and clear
Introduction to Kotlin: Brief and clear
 
Swift and Kotlin Presentation
Swift and Kotlin PresentationSwift and Kotlin Presentation
Swift and Kotlin Presentation
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 
Kotlin gets Reflection
Kotlin gets ReflectionKotlin gets Reflection
Kotlin gets Reflection
 

Similar a Flexible Types in Kotlin - JVMLS 2015

Javascript done right - Open Web Camp III
Javascript done right - Open Web Camp IIIJavascript done right - Open Web Camp III
Javascript done right - Open Web Camp III
Dirk Ginader
 
Server Side JavaScript - You ain't seen nothing yet
Server Side JavaScript - You ain't seen nothing yetServer Side JavaScript - You ain't seen nothing yet
Server Side JavaScript - You ain't seen nothing yet
Tom Croucher
 

Similar a Flexible Types in Kotlin - JVMLS 2015 (20)

Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2Apache Camel K - Copenhagen v2
Apache Camel K - Copenhagen v2
 
Apache Camel K - Copenhagen
Apache Camel K - CopenhagenApache Camel K - Copenhagen
Apache Camel K - Copenhagen
 
Excellent
ExcellentExcellent
Excellent
 
apachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdfapachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdf
 
2017-07-22 Common Workflow Language Viewer
2017-07-22 Common Workflow Language Viewer2017-07-22 Common Workflow Language Viewer
2017-07-22 Common Workflow Language Viewer
 
Attack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaAttack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and Kibana
 
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem novaKotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
Kotlin+MicroProfile: Ensinando 20 anos para uma linguagem nova
 
Security testing of YUI powered applications
Security testing of YUI powered applicationsSecurity testing of YUI powered applications
Security testing of YUI powered applications
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorld
 
Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017Develop your next app with kotlin @ AndroidMakersFr 2017
Develop your next app with kotlin @ AndroidMakersFr 2017
 
Javascript done right - Open Web Camp III
Javascript done right - Open Web Camp IIIJavascript done right - Open Web Camp III
Javascript done right - Open Web Camp III
 
Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.
 
Server Side JavaScript - You ain't seen nothing yet
Server Side JavaScript - You ain't seen nothing yetServer Side JavaScript - You ain't seen nothing yet
Server Side JavaScript - You ain't seen nothing yet
 
Writing your Third Plugin
Writing your Third PluginWriting your Third Plugin
Writing your Third Plugin
 
[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기
 
Web services and JavaScript
Web services and JavaScriptWeb services and JavaScript
Web services and JavaScript
 
Automated acceptance test
Automated acceptance testAutomated acceptance test
Automated acceptance test
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
«Продакшн в Kotlin DSL» Сергей Рыбалкин
«Продакшн в Kotlin DSL» Сергей Рыбалкин«Продакшн в Kotlin DSL» Сергей Рыбалкин
«Продакшн в Kotlin DSL» Сергей Рыбалкин
 
JavaScript Performance Patterns
JavaScript Performance PatternsJavaScript Performance Patterns
JavaScript Performance Patterns
 

Más de Andrey Breslav

Kotlin (Introduction for students)
Kotlin (Introduction for students)Kotlin (Introduction for students)
Kotlin (Introduction for students)
Andrey Breslav
 
Kotlin: Incompetence * Motivation = Innovation?
Kotlin: Incompetence * Motivation = Innovation?Kotlin: Incompetence * Motivation = Innovation?
Kotlin: Incompetence * Motivation = Innovation?
Andrey Breslav
 
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Andrey Breslav
 
JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
JavaOne2012: Kotlin: Practical Aspects of JVM Language ImplementationJavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
Andrey Breslav
 
[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop
Andrey Breslav
 
Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011
Andrey Breslav
 
Kotlin @ StrangeLoop 2011
Kotlin @ StrangeLoop 2011Kotlin @ StrangeLoop 2011
Kotlin @ StrangeLoop 2011
Andrey Breslav
 

Más de Andrey Breslav (14)

2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
2022 May - Shoulders of Giants - Amsterdam - Kotlin Dev Day.pdf
 
Shoulders of giants: Languages Kotlin learned from
Shoulders of giants: Languages Kotlin learned fromShoulders of giants: Languages Kotlin learned from
Shoulders of giants: Languages Kotlin learned from
 
Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?Future of Kotlin - How agile can language development be?
Future of Kotlin - How agile can language development be?
 
Eval4j @ JVMLS 2014
Eval4j @ JVMLS 2014Eval4j @ JVMLS 2014
Eval4j @ JVMLS 2014
 
Kotlin (Introduction for students)
Kotlin (Introduction for students)Kotlin (Introduction for students)
Kotlin (Introduction for students)
 
Language Design Trade-offs
Language Design Trade-offsLanguage Design Trade-offs
Language Design Trade-offs
 
Kotlin: Incompetence * Motivation = Innovation?
Kotlin: Incompetence * Motivation = Innovation?Kotlin: Incompetence * Motivation = Innovation?
Kotlin: Incompetence * Motivation = Innovation?
 
Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?Who's More Functional: Kotlin, Groovy, Scala, or Java?
Who's More Functional: Kotlin, Groovy, Scala, or Java?
 
JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
JavaOne2012: Kotlin: Practical Aspects of JVM Language ImplementationJavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
JavaOne2012: Kotlin: Practical Aspects of JVM Language Implementation
 
[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop[JVMLS 12] Kotlin / Java Interop
[JVMLS 12] Kotlin / Java Interop
 
Kotlin @ Devoxx 2011
Kotlin @ Devoxx 2011Kotlin @ Devoxx 2011
Kotlin @ Devoxx 2011
 
Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011Kotlin Slides from Devoxx 2011
Kotlin Slides from Devoxx 2011
 
Kotlin @ CSClub & Yandex
Kotlin @ CSClub & YandexKotlin @ CSClub & Yandex
Kotlin @ CSClub & Yandex
 
Kotlin @ StrangeLoop 2011
Kotlin @ StrangeLoop 2011Kotlin @ StrangeLoop 2011
Kotlin @ StrangeLoop 2011
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Flexible Types in Kotlin - JVMLS 2015