SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
This slide intentionally left blank.
Tricks with Types

       How to get fired
 with the Java type system.

            Shevek
      shevek@anarres.org
Compilers
●   The job of the compiler is turn your source into
    binary.

●   That's all, right?

●   No, it also helps you write correct code.

●   The type system is the most significant tool in the
    arsenal.
Java

●   Java is simple.
●   Java does not allow language extensions.
●   It has primitive types and classes.
●   It has single inheritance and interfaces.

●   So what can we do?

●   It doesn't get interesting yet.
Java 1.5

●   Parameterized types.
    ●     List<X>

        public interface Foo<X> {
            public void add(X value);
            public X get(int index);
        }

●   Now the compiler can check our code.
        Foo<String> x = ...;

        x.add(“bar”); // OK

        x.add(5);     // Not OK

        String value = x.get(4);   // Note, no cast.
Where Can We Use Parameters?
●   More places than you think!
     public class Foo<X> { // Here, we all know.

         @Override
         public <T> T add(Foo<T> remote, T value) { // Also, here!
             ...
         }
     }



●   Now we can say “These two things are of the
    same type.” without knowing the type!
Java 1.5 Bytecode
●   What happens underneath?
    public interface Foo<X> {
        public void add(X value); // It's an Object.
    }

    public class MyFoo implements Foo<String> {
        @Override
        public void add(String value) { // This can't override (Object)
              ...
        }
    }

    public class MyFoo implements Foo<String> {

        public void add(String value) {
            ...
        }

        @Override
        public synthetic void add(Object value) {// So this does.
            add((String)value);
        }
    }
Bounded Parameters
●   We can give required properties of the
    parameter X.
    public interface Foo<X extends Bar> {
        public void add(X value) {
              // Now we can use the properties of Bar, but not X.
        }
    }

    public class MyBar extends Bar { }
    public class YourBar extends Bar { }

    Foo<MyBar>        // Valid
    Foo<YourBar>      // Valid
    Foo<String>       // Invalid
More Power to Type Bounds
●   Help us write correct code.
    public interface MyContainer<X> {
        public List<X> void getValues();
    }

    MyContainer<String> x = …;
    List<String> l = x.getValues();
    x.add(“foo”);

●   Did we just modify an internal data structure?
●   Can the compiler help us find out?
    public interface MyContainer<X> {
        public List<? extends X> void getValues();
    }

    MyContainer<String> x = …;
    List<? extends String> l = x.getValues();
    x.add(“foo”); // Illegal – can't create a value of type unknown.
Even More Power to Type Bounds
●   We did read-only. Can we do write-only?
    public interface MyContainer<X> {
        public List<? super X> void getTarget();
    }

    MyContainer<String> x = …;
    List<String> l = x.getTarget();
    x.add(“foo”); // We're allowed to add Strings, or anything below.
    x.get(...);   // Illegal, since we don't know the return type.
What Does a Bound Tell Us?
●   It doesn't tell us the type, just the properties.
●   We can have multiple bounds!
     public interface MyContainer {
         public <T extends JComponent & MyPanel> void add(T panel) {
               // Now we can use the properties of JComponent
               // and MyPanel.
         }
     }


●   Now, we specified multiple behaviours in a
    language with only single inheritance!
●   I forget what bytecode it compiles here.
Types Are Powerful
●   Types are the primary tool for the compiler to
    prove correctness of code.

●   If you used a cast, you did something wrong.

●   Say what you mean, and the rest will follow.
Thank you




            Guh.....? What just happened?

Contenu connexe

Tendances

Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building BlocksCate Huston
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on PythonSumit Raj
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScriptRajat Saxena
 
Cross platform native development in f#
Cross platform native development in f#Cross platform native development in f#
Cross platform native development in f#David Kay
 
Unsafe to typesafe
Unsafe to typesafeUnsafe to typesafe
Unsafe to typesaferegisleray
 
Optionals by Matt Faluotico
Optionals by Matt FaluoticoOptionals by Matt Faluotico
Optionals by Matt FaluoticoWithTheBest
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless Jared Roesch
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into javaSadhanaParameswaran
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 

Tendances (20)

Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Hands on Session on Python
Hands on Session on PythonHands on Session on Python
Hands on Session on Python
 
Lecture5
Lecture5Lecture5
Lecture5
 
Datatype in JavaScript
Datatype in JavaScriptDatatype in JavaScript
Datatype in JavaScript
 
Pointers & functions
Pointers &  functionsPointers &  functions
Pointers & functions
 
Lesson 2 php data types
Lesson 2   php data typesLesson 2   php data types
Lesson 2 php data types
 
Cross platform native development in f#
Cross platform native development in f#Cross platform native development in f#
Cross platform native development in f#
 
Java Basic day-1
Java Basic day-1Java Basic day-1
Java Basic day-1
 
Unsafe to typesafe
Unsafe to typesafeUnsafe to typesafe
Unsafe to typesafe
 
Optionals by Matt Faluotico
Optionals by Matt FaluoticoOptionals by Matt Faluotico
Optionals by Matt Faluotico
 
Os Welton
Os WeltonOs Welton
Os Welton
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
PHP
 PHP PHP
PHP
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into java
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
class and objects
class and objectsclass and objects
class and objects
 
Effective PHP. Part 2
Effective PHP. Part 2Effective PHP. Part 2
Effective PHP. Part 2
 

En vedette

Digital Angle Overview
Digital Angle   OverviewDigital Angle   Overview
Digital Angle OverviewEamon Tuhami
 
Desfile 7 de setembro 2012 - Escola Municipal Antonio Rayol
Desfile 7 de setembro 2012 - Escola Municipal Antonio RayolDesfile 7 de setembro 2012 - Escola Municipal Antonio Rayol
Desfile 7 de setembro 2012 - Escola Municipal Antonio RayolThais Beatriz
 
Viral seeding creds 2015
Viral seeding creds 2015Viral seeding creds 2015
Viral seeding creds 2015viralseeding
 
English
EnglishEnglish
Englishrouns
 
Newer film programming content type ii
Newer film programming content type iiNewer film programming content type ii
Newer film programming content type iiDeepak Somaji-Sawant
 
Ordering numbers
Ordering numbersOrdering numbers
Ordering numberscarinmcatee
 
Grow with Gemara
Grow with GemaraGrow with Gemara
Grow with Gemaraperlmutter
 
Newer film programming content concept
Newer film programming content conceptNewer film programming content concept
Newer film programming content conceptDeepak Somaji-Sawant
 
Johnny english film direction quality points
Johnny english film direction  quality points Johnny english film direction  quality points
Johnny english film direction quality points Deepak Somaji-Sawant
 
^^ Film director recognition could be from me ^^ .
^^  Film director recognition could be  from me ^^ .^^  Film director recognition could be  from me ^^ .
^^ Film director recognition could be from me ^^ .Deepak Somaji-Sawant
 
Film direction quality points receiw for the muppets english film
Film direction quality points receiw for  the muppets  english film  Film direction quality points receiw for  the muppets  english film
Film direction quality points receiw for the muppets english film Deepak Somaji-Sawant
 
Energysources
EnergysourcesEnergysources
Energysourcesrouns
 
Grow With Gemara
Grow With GemaraGrow With Gemara
Grow With Gemaraperlmutter
 
Презентация+портфолио дизайн-студии "Кефир" (Харьков)
Презентация+портфолио дизайн-студии "Кефир" (Харьков)Презентация+портфолио дизайн-студии "Кефир" (Харьков)
Презентация+портфолио дизайн-студии "Кефир" (Харьков)Kefir Design Studio
 

En vedette (16)

Digital Angle Overview
Digital Angle   OverviewDigital Angle   Overview
Digital Angle Overview
 
Desfile 7 de setembro 2012 - Escola Municipal Antonio Rayol
Desfile 7 de setembro 2012 - Escola Municipal Antonio RayolDesfile 7 de setembro 2012 - Escola Municipal Antonio Rayol
Desfile 7 de setembro 2012 - Escola Municipal Antonio Rayol
 
Viral seeding creds 2015
Viral seeding creds 2015Viral seeding creds 2015
Viral seeding creds 2015
 
Money ball english film reveiw
Money ball english film reveiw Money ball english film reveiw
Money ball english film reveiw
 
English
EnglishEnglish
English
 
Newer film programming content type ii
Newer film programming content type iiNewer film programming content type ii
Newer film programming content type ii
 
Ordering numbers
Ordering numbersOrdering numbers
Ordering numbers
 
Grow with Gemara
Grow with GemaraGrow with Gemara
Grow with Gemara
 
Newer film programming content concept
Newer film programming content conceptNewer film programming content concept
Newer film programming content concept
 
Johnny english film direction quality points
Johnny english film direction  quality points Johnny english film direction  quality points
Johnny english film direction quality points
 
^^ Film director recognition could be from me ^^ .
^^  Film director recognition could be  from me ^^ .^^  Film director recognition could be  from me ^^ .
^^ Film director recognition could be from me ^^ .
 
English film reveiw war horse
English film reveiw war horse  English film reveiw war horse
English film reveiw war horse
 
Film direction quality points receiw for the muppets english film
Film direction quality points receiw for  the muppets  english film  Film direction quality points receiw for  the muppets  english film
Film direction quality points receiw for the muppets english film
 
Energysources
EnergysourcesEnergysources
Energysources
 
Grow With Gemara
Grow With GemaraGrow With Gemara
Grow With Gemara
 
Презентация+портфолио дизайн-студии "Кефир" (Харьков)
Презентация+портфолио дизайн-студии "Кефир" (Харьков)Презентация+портфолио дизайн-студии "Кефир" (Харьков)
Презентация+портфолио дизайн-студии "Кефир" (Харьков)
 

Similaire à Getting Fired with Java Types

Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyHaim Yadid
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharpDaniele Pozzobon
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in Kotlin7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in KotlinLuca Guadagnini
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.pptMikeAdva
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golangBasil N G
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Chris Adamson
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdfPuneetChaturvedi23
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdfSpam92
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#Alfonso Garcia-Caro
 

Similaire à Getting Fired with Java Types (20)

Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, Seriously
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in Kotlin7 Sins of Java fixed in Kotlin
7 Sins of Java fixed in Kotlin
 
Java
JavaJava
Java
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdf
 
golang_refcard.pdf
golang_refcard.pdfgolang_refcard.pdf
golang_refcard.pdf
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
Functional Programming in C# and F#
Functional Programming in C# and F#Functional Programming in C# and F#
Functional Programming in C# and F#
 

Dernier

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Dernier (20)

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

Getting Fired with Java Types

  • 2. Tricks with Types How to get fired with the Java type system. Shevek shevek@anarres.org
  • 3. Compilers ● The job of the compiler is turn your source into binary. ● That's all, right? ● No, it also helps you write correct code. ● The type system is the most significant tool in the arsenal.
  • 4. Java ● Java is simple. ● Java does not allow language extensions. ● It has primitive types and classes. ● It has single inheritance and interfaces. ● So what can we do? ● It doesn't get interesting yet.
  • 5. Java 1.5 ● Parameterized types. ● List<X> public interface Foo<X> { public void add(X value); public X get(int index); } ● Now the compiler can check our code. Foo<String> x = ...; x.add(“bar”); // OK x.add(5); // Not OK String value = x.get(4); // Note, no cast.
  • 6. Where Can We Use Parameters? ● More places than you think! public class Foo<X> { // Here, we all know. @Override public <T> T add(Foo<T> remote, T value) { // Also, here! ... } } ● Now we can say “These two things are of the same type.” without knowing the type!
  • 7. Java 1.5 Bytecode ● What happens underneath? public interface Foo<X> { public void add(X value); // It's an Object. } public class MyFoo implements Foo<String> { @Override public void add(String value) { // This can't override (Object) ... } } public class MyFoo implements Foo<String> { public void add(String value) { ... } @Override public synthetic void add(Object value) {// So this does. add((String)value); } }
  • 8. Bounded Parameters ● We can give required properties of the parameter X. public interface Foo<X extends Bar> { public void add(X value) { // Now we can use the properties of Bar, but not X. } } public class MyBar extends Bar { } public class YourBar extends Bar { } Foo<MyBar> // Valid Foo<YourBar> // Valid Foo<String> // Invalid
  • 9. More Power to Type Bounds ● Help us write correct code. public interface MyContainer<X> { public List<X> void getValues(); } MyContainer<String> x = …; List<String> l = x.getValues(); x.add(“foo”); ● Did we just modify an internal data structure? ● Can the compiler help us find out? public interface MyContainer<X> { public List<? extends X> void getValues(); } MyContainer<String> x = …; List<? extends String> l = x.getValues(); x.add(“foo”); // Illegal – can't create a value of type unknown.
  • 10. Even More Power to Type Bounds ● We did read-only. Can we do write-only? public interface MyContainer<X> { public List<? super X> void getTarget(); } MyContainer<String> x = …; List<String> l = x.getTarget(); x.add(“foo”); // We're allowed to add Strings, or anything below. x.get(...); // Illegal, since we don't know the return type.
  • 11. What Does a Bound Tell Us? ● It doesn't tell us the type, just the properties. ● We can have multiple bounds! public interface MyContainer { public <T extends JComponent & MyPanel> void add(T panel) { // Now we can use the properties of JComponent // and MyPanel. } } ● Now, we specified multiple behaviours in a language with only single inheritance! ● I forget what bytecode it compiles here.
  • 12. Types Are Powerful ● Types are the primary tool for the compiler to prove correctness of code. ● If you used a cast, you did something wrong. ● Say what you mean, and the rest will follow.
  • 13. Thank you Guh.....? What just happened?