Top Banner
Evolving the Java Programming Language Neal Gafter
42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Evolving The Java Language

Evolvingthe

Java Programming Language

Neal Gafter

Page 2: Evolving The Java Language

Overview

The Challenge of Evolving a LanguageDesign PrinciplesDesign GoalsJDK7 and JDK8

Page 3: Evolving The Java Language

Challenge: Evolving a Language

“What is it like trying to extend a mature lan-guage?” -Brian Goetz

Page 4: Evolving The Java Language

Challenge: Evolving a Language

“What is it like trying to extend a mature lan-guage?”

ma-ture: adjective1. having completed natural growth and de-

velopment.2. completed, perfected, or elaborated in full.3. (of an industry, technology, market, etc.) no

longer developing or expanding.

Page 5: Evolving The Java Language

Challenge: Evolving a Language

Q: What is it like trying to extend a mature language?

A: It is impossible, by definition.

Page 6: Evolving The Java Language

Challenge: Evolving a Language

Q: What is it like trying to extend a widely de-ployed language?

A: Language change is influenced by existing code and APIs.

Page 7: Evolving The Java Language

Existing APIs affect change

Support retrofitting existing APIs:With compatible behavior

Page 8: Evolving The Java Language

Existing APIs affect change

Support retrofitting existing APIs:With compatible behavior

Consistent with existing designDon't expose, create design flaws

Page 9: Evolving The Java Language

Existing APIs affect change

Some case studies:

Generics (vs erasure)Wildcards (vs declaration-site variance)Autoboxing, unboxing (vs wrappers)Varargs (vs overloading)For-each (vs Iterator)

Page 10: Evolving The Java Language

Generics (vs erasure)

Adding reified generics is compatibleMay not allow retrofitting existing code

CollectionWeakReference

Page 11: Evolving The Java Language

Wildcards (vs declaration-site variance)

There is a simpler alternative to wildcards: declaration-site variance

'wildcards' appear on type definitionsUse sites much simplerCan retrofit most APIs

but not Collection

Page 12: Evolving The Java Language

Autoboxing, unboxing

Type systems form a lattice

Object / \Number Runnable | /Integer / \ / null

Page 13: Evolving The Java Language

Autoboxing, unboxing

Adding conversions can break the lattice

long <----------> Long | | | |int <----------> Integer

Existing boxed types don't have this relation.

Page 14: Evolving The Java Language

Autoboxing, unboxing

Solutions

Introduce new boxing interfaces; orPatchwork specification

Page 15: Evolving The Java Language

for-each (vs iterator)

Iterator has a vestigal remove method.Introduce java.lang.Iterator without it?Cannot retrofit Collection

without requiring recompile

(existing implementations don't implementiterator() that returns the new type)

Page 16: Evolving The Java Language

Design Principles

Page 17: Evolving The Java Language

Design Principles

Encourage desirable practices (that might not otherwise be followed) my making them easy

synchronizedAnnotations isolate configuration dataGenerics for typesafe APIs

Page 18: Evolving The Java Language

Design Principles

Encourage desirable practicesIsolate language from specific APIs

Page 19: Evolving The Java Language

Design Principles

Encourage desirable practicesIsolate language from specific APIsPrefer reading over writing

clarity over conciseness

Page 20: Evolving The Java Language

Design Principles

Encourage desirable practicesIsolate language from specific APIsPrefer reading over writingPrefer static typing

Page 21: Evolving The Java Language

Design Principles

Encourage desirable practicesIsolate language from specific APIsPrefer reading over writingPrefer static typingRemain backward compatible

Page 22: Evolving The Java Language

Short-term design Goals

Regularize Existing LanguageImprove diagnostics vs genericsFix type inferenceString switchLimited operator overloadingImproved catch clauses

Modularity

Page 23: Evolving The Java Language

Improve Diagnostics vs Generics

interface Box<T> {

T get();

void put(T t);

}class Tmp {

static void test(Box<? extends Number> box) {

box.put(box.get());

}

}

Error: put(capture#417 of ? extends java.lang.Number)

in Box<capture#417 of ? extends java.lang.Number> cannot be applied to (java.lang.Number)

box.put(box.get());

^

Page 24: Evolving The Java Language

Improve Diagnostics vs Generics

interface Box<T> {

T get();

void put(T t);

}class Tmp {

static void test(Box<? extends Number> box) {

box.put(box.get());

}

}

Error: cannot call put(T) as a member of in Box<? ex-

tends java.lang.Number> box.put(box.get());

^

Page 25: Evolving The Java Language

Fix Type Inference: constructors

Today:

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

Page 26: Evolving The Java Language

Fix Type Inference: constructors

Proposed:

Map<String, List<String>> anagrams = new HashMap<>();

Page 27: Evolving The Java Language

Fix Type Inference: arguments

Today:

public <E> Set<E> emptySet() { … }

void timeWaitsFor(Set<Man> people) { … }

// * Won't compile!

timeWaitsFor(Collections.emptySet());

Page 28: Evolving The Java Language

Fix Type Inference: arguments

Today:

public <E> Set<E> emptySet() { … }

void timeWaitsFor(Set<Man> people) { … }

// OK

timeWaitsFor(Collections.<Man>emptySet());

Page 29: Evolving The Java Language

Fix Type Inference: arguments

Proposed:

public <E> Set<E> emptySet() { … }

void timeWaitsFor(Set<Man> people) { … }

// OK

timeWaitsFor(Collections.emptySet());

Page 30: Evolving The Java Language

String Switch

Today

static boolean booleanFromString(String s) {

if (s.equals("true")) {

return true;

} else if (s.equals("false")) {

return false;

} else {

throw new IllegalArgumentException(s);

}

}

Page 31: Evolving The Java Language

String Switch

Proposed

static boolean booleanFromString(String s) {

switch(s) {

case "true":

return true;

case "false":

return false;

default:

throw new IllegalArgumentException(s);

}

}

Page 32: Evolving The Java Language

Limited Operator Overloading

Today

enum Size { SMALL, MEDIUM, LARGE }

if (mySize.compareTo(yourSize) >= 0)

System.out.println(“You can wear my pants.”);

Page 33: Evolving The Java Language

Limited Operator Overloading

Proposed

enum Size { SMALL, MEDIUM, LARGE }

if (mySize > yourSize)

System.out.println(“You can wear my pants.”);

Page 34: Evolving The Java Language

Improved Catch Clauses: multi

Today:

try {

return klass.newInstance();

} catch (InstantiationException e) {

throw new AssertionError(e);

} catch (IllegalAccessException e) {

throw new AssertionError(e);

}

Page 35: Evolving The Java Language

Improved Catch Clauses: multi

Proposed:

try {

return klass.newInstance();

} catch (InstantiationException | IllegalAccessException e) {

throw new AssertionError(e);

}

Page 36: Evolving The Java Language

Improved Catch Clauses: rethrow

Today:

try {

doable.doit(); // Throws several types

} catch (Throwable ex) {

logger.log(ex);

throw ex; // Error: Throwable not declared

}

Page 37: Evolving The Java Language

Improved Catch Clauses: rethrow

Proposed:

try {

doable.doit(); // Throws several types

} catch (final Throwable ex) {

logger.log(ex);

throw ex; // OK: Throws the same several types

}

Page 38: Evolving The Java Language

Others?

Properties?Serialization annotations?

Page 39: Evolving The Java Language

Long-term goals

Regularize Existing LanguageReificationFurther Generics simplification

Concurrency supportImmutable dataControl AbstractionClosuresActors, etc.

Page 40: Evolving The Java Language

JDK7

jsrs 277 + 294 (modularity)Maintenance review of jsr14“Small” language issuesPossibly jsr308

(limited by time, resources)

Page 41: Evolving The Java Language

JDK8

ReificationControl Abstraction

Further out: Immutable data, pattern match-ing, further operator overloading?

Page 42: Evolving The Java Language

Q&A