SlideShare a Scribd company logo
1 of 36
Download to read offline
Fire in the type hole
Why you shouldn’t trust a Java compiler in everything
Heejong Lee (@heejongl)
– Mr. Unknown Java Compiler
“Do you trust me?”
Computer
Science
[Tool]
The Study of
Programming Languages
[Method]
The Study of Algorithm
• Reliable safety net for programming systems
• Statically verifying that something wrong will not
happen in the runtime
• Blazingly fast
A great step forward to the better world :
Type System
Type Systems
Type System
Safe programs Unsafe programs
Typed programs
• Verified by a type system implies a program will
never violate the rule that the type system
defines
• Failed by a type system does not implies a
program will violate the rule that the type system
defines
• Possibly, there exists a program which is safe
but a type system cannot verify its safety
Type System
Type System
1
Type System
1:int
Type System
1:int + “hello”
Type System
1:int + “hello”:string
Type System
1:int + “hello”:string
May cause segmentation fault, exception,
unexpected behavior in some languages
Type System
(1:int + “hello”:string):string
In Java, 1 will be automatically converted to string
Type System
(1:int + “hello”:string):int
Or, error: incompatible types: String cannot be
converted to int
Type System
String foo(int x) {
int y = 10 + x;
return y.toString();
}
void bar() {
foo(10);
}
foo : int -> string
= +
toString
bar : unit -> unit
foo( )
“…Really?”
One value to rule types all
• The most catastrophic design decision ever
made
• Tony Hoare introduced Null references in ALGOL
W back in 1965 “simply because it was so easy
to implement”
• Can be an any type, ultimately unarm the Java
type system
Null
foo : string -> string
bar : unit -> unit
foo( )
Type System
String foo(String x) {
return x.toUpperCase();
}
void bar() {
String s = null;
foo(s);
}
toUpperCase
foo : string -> string
bar : unit -> unit
foo( )
Type System
String foo(String x) {
return x.toUpperCase();
}
void bar() {
String s = null;
foo(s);
}
toUpperCase
• Impossible in general
• However, recent programming languages
introduce a typed null : Option[T] in Scala,
Optional<T> in Java8
How could we survive Null attacks
Null Checking Idioms
String x = getString();
String y = null;
if(x != null) {
y = x.toUpperCase();
} else {
y = “none”;
}
val x = Option(getString())
val y =
x.map{_.toUpperCase}.
getOrElse(“none”)
Java 7- Scala
Null Checking Idioms
String x = getString();
String y = x.toUpperCase();
val x = Option(getString())
val y = x.toUpperCase
Java 7- Scala
Okay until it actually runs Compilation error
• Use static analyzers. Not perfect but practical
enough
• Do not use null at all. Define empty values. Use
alternative languages such as Scala
Possible Solutions for Null
Game is not over yet
Long time ago in the Java type system,
Java Array and Type Variance
String[] strings = new String[10];
Object[] objects = strings;
objects[0] = 0;
Compiled well but throw exception in a runtime
Why?
Type Variance
Set[Dog] <: Set[Animal] ?
Assume Dog is a subclass of Animal
Could a set of Dog be a subclass of a set of Animal?
Dog <: Animal
Type Variance
Set[Dog] <: Set[Animal]
Assume Dog is a subclass of Animal
If Set is covariant
Dog <: Animal
If Set is contravariant
Set[Animal] <: Set[Dog]
Type Variance in Action
Assume Dog <: Animal, Rose <: Flower
Animal RoseDog Flower
We can use a function Animal -> Rose as if it were a function Dog -> Flower
Type Variance in Action
Assume Dog <: Animal, Rose <: Flower
Animal RoseDog Flower
val foo : Animal => Rose = { x => …}
val bar : Dog => Flower = foo
Type Variance in Action
Assume Dog <: Animal, Rose <: Flower
Animal RoseDog Flower
val foo : Function1[Animal,Rose] = { x => …}
val bar : Function1[Dog,Flower] = foo
trait Function1[-T,+R]
Java Array Revisited
String[] strings = new String[10];
Object[] objects = strings;
objects[0] = 0;
Java Array is covariant which means
Reading from Java Array is always safe
Writing to Java Array is not always safe
Type Variance Revisited
Variance Explanation Reading Writing
Set[E]
Invariance
Set[Animal] should be Set[Animal]
Set[Dog] should be Set[Dog]
Safe Safe
Set[+E]
Covariance
Set[Animal] can be Set[Dog]
Set[Dog] should be Set[Dog]
Safe Unsafe
Set[-E]
Contravariance
Set[Animal] should be Set[Animal]
Set[Dog] can be Set[Animal]
Unsafe Safe
Assume Dog is a subclass of Animal
• Use generics. Avoid raw types. Avoid arrays
• Use alternative type-safe languages such as
Scala
Possible Solutions for Java Type Holes
Deus ex machina
Just use Scala
http://scala-lang.org

More Related Content

What's hot

Software Development Lab test 3
Software Development Lab test 3 Software Development Lab test 3
Software Development Lab test 3 Alvin Chin
 
Pontificating quantification
Pontificating quantificationPontificating quantification
Pontificating quantificationAaron Bedra
 
NSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to SwiftNSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to SwiftAndreas Blick
 
String interpolation
String interpolationString interpolation
String interpolationKnoldus Inc.
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...Doris Chen
 
Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5Berk Soysal
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUIBongwon Lee
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scalafanf42
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?Sarah Mount
 
Making friends with TDD
Making friends with TDDMaking friends with TDD
Making friends with TDDJohn Cleary
 
Annotation based null analysis in Eclipse JDT
Annotation based null analysis in Eclipse JDTAnnotation based null analysis in Eclipse JDT
Annotation based null analysis in Eclipse JDTEclipse Day India
 
Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Ganesh Samarthyam
 
Futzing with actors (etc.)
Futzing with actors (etc.)Futzing with actors (etc.)
Futzing with actors (etc.)league
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8James Brown
 

What's hot (20)

Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
 
Software Development Lab test 3
Software Development Lab test 3 Software Development Lab test 3
Software Development Lab test 3
 
Pontificating quantification
Pontificating quantificationPontificating quantification
Pontificating quantification
 
NSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to SwiftNSCoder Swift - An Introduction to Swift
NSCoder Swift - An Introduction to Swift
 
String interpolation
String interpolationString interpolation
String interpolation
 
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...OSCON Presentation: Developing High Performance Websites and Modern Apps with...
OSCON Presentation: Developing High Performance Websites and Modern Apps with...
 
Java Tutorial Lab 5
Java Tutorial Lab 5Java Tutorial Lab 5
Java Tutorial Lab 5
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Developing a new Epsilon EMC driver
Developing a new Epsilon EMC driverDeveloping a new Epsilon EMC driver
Developing a new Epsilon EMC driver
 
A Tour Of Scala
A Tour Of ScalaA Tour Of Scala
A Tour Of Scala
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Scala - just good for Java shops?
Scala - just good for Java shops?Scala - just good for Java shops?
Scala - just good for Java shops?
 
Making friends with TDD
Making friends with TDDMaking friends with TDD
Making friends with TDD
 
Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Annotation based null analysis in Eclipse JDT
Annotation based null analysis in Eclipse JDTAnnotation based null analysis in Eclipse JDT
Annotation based null analysis in Eclipse JDT
 
Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8Functional Thinking - Programming with Lambdas in Java 8
Functional Thinking - Programming with Lambdas in Java 8
 
Akka 2.0 Reloaded
Akka 2.0 ReloadedAkka 2.0 Reloaded
Akka 2.0 Reloaded
 
Futzing with actors (etc.)
Futzing with actors (etc.)Futzing with actors (etc.)
Futzing with actors (etc.)
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
 

Similar to Fire in the type hole

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Yevhen Bobrov
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesDebasish Ghosh
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldWerner Hofstra
 
Next generation actors with Akka
Next generation actors with AkkaNext generation actors with Akka
Next generation actors with AkkaJohan Andrén
 
Haskell for Scala-ists
Haskell for Scala-istsHaskell for Scala-ists
Haskell for Scala-istschriseidhof
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard LibrarySantosh Rajan
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#Remik Koczapski
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaVasil Remeniuk
 
Python introduction
Python introductionPython introduction
Python introductionleela rani
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsCodeOps Technologies LLP
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOJorge Vásquez
 

Similar to Fire in the type hole (20)

Java introduction
Java introductionJava introduction
Java introduction
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015
 
Property based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rulesProperty based Testing - generative data & executable domain rules
Property based Testing - generative data & executable domain rules
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Next generation actors with Akka
Next generation actors with AkkaNext generation actors with Akka
Next generation actors with Akka
 
Haskell for Scala-ists
Haskell for Scala-istsHaskell for Scala-ists
Haskell for Scala-ists
 
The Swift Compiler and Standard Library
The Swift Compiler and Standard LibraryThe Swift Compiler and Standard Library
The Swift Compiler and Standard Library
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Type Classes
Type ClassesType Classes
Type Classes
 
ppopoff
ppopoffppopoff
ppopoff
 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#
 
Types by Adform Research, Saulius Valatka
Types by Adform Research, Saulius ValatkaTypes by Adform Research, Saulius Valatka
Types by Adform Research, Saulius Valatka
 
Python introduction
Python introductionPython introduction
Python introduction
 
Functional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and StreamsFunctional Programming in Java 8 - Lambdas and Streams
Functional Programming in Java 8 - Lambdas and Streams
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 

Recently uploaded

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 

Recently uploaded (20)

What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024VictoriaMetrics Anomaly Detection Updates: Q1 2024
VictoriaMetrics Anomaly Detection Updates: Q1 2024
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 

Fire in the type hole

  • 1. Fire in the type hole Why you shouldn’t trust a Java compiler in everything Heejong Lee (@heejongl)
  • 2. – Mr. Unknown Java Compiler “Do you trust me?”
  • 3. Computer Science [Tool] The Study of Programming Languages [Method] The Study of Algorithm
  • 4. • Reliable safety net for programming systems • Statically verifying that something wrong will not happen in the runtime • Blazingly fast A great step forward to the better world : Type System
  • 6. Type System Safe programs Unsafe programs Typed programs
  • 7. • Verified by a type system implies a program will never violate the rule that the type system defines • Failed by a type system does not implies a program will violate the rule that the type system defines • Possibly, there exists a program which is safe but a type system cannot verify its safety Type System
  • 10. Type System 1:int + “hello”
  • 11. Type System 1:int + “hello”:string
  • 12. Type System 1:int + “hello”:string May cause segmentation fault, exception, unexpected behavior in some languages
  • 13. Type System (1:int + “hello”:string):string In Java, 1 will be automatically converted to string
  • 14. Type System (1:int + “hello”:string):int Or, error: incompatible types: String cannot be converted to int
  • 15. Type System String foo(int x) { int y = 10 + x; return y.toString(); } void bar() { foo(10); } foo : int -> string = + toString bar : unit -> unit foo( )
  • 17. One value to rule types all
  • 18. • The most catastrophic design decision ever made • Tony Hoare introduced Null references in ALGOL W back in 1965 “simply because it was so easy to implement” • Can be an any type, ultimately unarm the Java type system Null
  • 19. foo : string -> string bar : unit -> unit foo( ) Type System String foo(String x) { return x.toUpperCase(); } void bar() { String s = null; foo(s); } toUpperCase
  • 20. foo : string -> string bar : unit -> unit foo( ) Type System String foo(String x) { return x.toUpperCase(); } void bar() { String s = null; foo(s); } toUpperCase
  • 21. • Impossible in general • However, recent programming languages introduce a typed null : Option[T] in Scala, Optional<T> in Java8 How could we survive Null attacks
  • 22. Null Checking Idioms String x = getString(); String y = null; if(x != null) { y = x.toUpperCase(); } else { y = “none”; } val x = Option(getString()) val y = x.map{_.toUpperCase}. getOrElse(“none”) Java 7- Scala
  • 23. Null Checking Idioms String x = getString(); String y = x.toUpperCase(); val x = Option(getString()) val y = x.toUpperCase Java 7- Scala Okay until it actually runs Compilation error
  • 24. • Use static analyzers. Not perfect but practical enough • Do not use null at all. Define empty values. Use alternative languages such as Scala Possible Solutions for Null
  • 25. Game is not over yet
  • 26. Long time ago in the Java type system,
  • 27. Java Array and Type Variance String[] strings = new String[10]; Object[] objects = strings; objects[0] = 0; Compiled well but throw exception in a runtime Why?
  • 28. Type Variance Set[Dog] <: Set[Animal] ? Assume Dog is a subclass of Animal Could a set of Dog be a subclass of a set of Animal? Dog <: Animal
  • 29. Type Variance Set[Dog] <: Set[Animal] Assume Dog is a subclass of Animal If Set is covariant Dog <: Animal If Set is contravariant Set[Animal] <: Set[Dog]
  • 30. Type Variance in Action Assume Dog <: Animal, Rose <: Flower Animal RoseDog Flower We can use a function Animal -> Rose as if it were a function Dog -> Flower
  • 31. Type Variance in Action Assume Dog <: Animal, Rose <: Flower Animal RoseDog Flower val foo : Animal => Rose = { x => …} val bar : Dog => Flower = foo
  • 32. Type Variance in Action Assume Dog <: Animal, Rose <: Flower Animal RoseDog Flower val foo : Function1[Animal,Rose] = { x => …} val bar : Function1[Dog,Flower] = foo trait Function1[-T,+R]
  • 33. Java Array Revisited String[] strings = new String[10]; Object[] objects = strings; objects[0] = 0; Java Array is covariant which means Reading from Java Array is always safe Writing to Java Array is not always safe
  • 34. Type Variance Revisited Variance Explanation Reading Writing Set[E] Invariance Set[Animal] should be Set[Animal] Set[Dog] should be Set[Dog] Safe Safe Set[+E] Covariance Set[Animal] can be Set[Dog] Set[Dog] should be Set[Dog] Safe Unsafe Set[-E] Contravariance Set[Animal] should be Set[Animal] Set[Dog] can be Set[Animal] Unsafe Safe Assume Dog is a subclass of Animal
  • 35. • Use generics. Avoid raw types. Avoid arrays • Use alternative type-safe languages such as Scala Possible Solutions for Java Type Holes
  • 36. Deus ex machina Just use Scala http://scala-lang.org