Neal Gafter Java Evolution

42
Evolving the Java Programming Language Neal Gafter

Transcript of Neal Gafter Java Evolution

Page 1: Neal Gafter Java Evolution

Evo

lvin

gth

eJa

va P

rog

ram

min

g L

ang

uag

e

Nea

l Gaf

ter

Page 2: Neal Gafter Java Evolution

Ove

rvie

w

The

Cha

lleng

e of

Evo

lvin

g a

Lang

uage

Des

ign

Prin

cipl

esD

esig

n G

oals

JDK

7 an

d JD

K8

Page 3: Neal Gafter Java Evolution

Ch

alle

ng

e: E

volv

ing

a L

ang

uag

e

“Wha

t is

it lik

e tr

ying

to e

xten

d a

mat

ure

lan-

guag

e?”

-B

rian

Goe

tz

Page 4: Neal Gafter Java Evolution

Ch

alle

ng

e: E

volv

ing

a L

ang

uag

e

“Wha

t is

it lik

e tr

ying

to e

xten

d a

mat

ure

lan-

guag

e?”

ma-

ture

: adj

ectiv

e1.

hav

ing

com

plet

ed n

atur

al g

row

th a

nd d

e-ve

lopm

ent.

2. c

ompl

eted

, per

fect

ed, o

r el

abor

ated

in fu

ll.3.

(of

an

indu

stry

, tec

hnol

ogy,

mar

ket,

etc.

) no

lo

nger

dev

elop

ing

or e

xpan

ding

.

Page 5: Neal Gafter Java Evolution

Ch

alle

ng

e: E

volv

ing

a L

ang

uag

e

Q: W

hat i

s it

like

tryi

ng to

ext

end

a m

atur

e la

ngua

ge?

A: I

t is

impo

ssib

le, b

y de

finiti

on.

Page 6: Neal Gafter Java Evolution

Ch

alle

ng

e: E

volv

ing

a L

ang

uag

e

Q: W

hat i

s it

like

tryi

ng to

ext

end

a w

idel

y de

-pl

oyed

lang

uage

?

A: L

angu

age

chan

ge is

influ

ence

d by

exi

stin

g co

de a

nd A

PIs

.

Page 7: Neal Gafter Java Evolution

Exi

stin

g A

PIs

aff

ect

chan

ge

Sup

port

ret

rofit

ting

exis

ting

AP

Is:

With

com

patib

le b

ehav

ior

Page 8: Neal Gafter Java Evolution

Exi

stin

g A

PIs

aff

ect

chan

ge

Sup

port

ret

rofit

ting

exis

ting

AP

Is:

With

com

patib

le b

ehav

ior

Con

sist

ent w

ith e

xist

ing

desi

gnD

on't

expo

se, c

reat

e de

sign

flaw

s

Page 9: Neal Gafter Java Evolution

Exi

stin

g A

PIs

aff

ect

chan

ge

Som

e ca

se s

tudi

es:

Gen

eric

s (v

s er

asur

e)W

ildca

rds

(vs

decl

arat

ion-

site

var

ianc

e)A

utob

oxin

g, u

nbox

ing

(vs

wra

pper

s)V

arar

gs (

vs o

verlo

adin

g)F

or-e

ach

(vs

Itera

tor)

Page 10: Neal Gafter Java Evolution

Gen

eric

s (v

s er

asu

re)

Add

ing

reifi

ed g

ener

ics

is c

ompa

tible

May

not

allo

w r

etro

fittin

g ex

istin

g co

deC

olle

ctio

nW

eakR

efer

ence

Page 11: Neal Gafter Java Evolution

Wild

card

s (v

s d

ecla

rati

on

-sit

e va

rian

ce)

The

re is

a s

impl

er a

ltern

ativ

e to

wild

card

s:

decl

arat

ion-

site

var

ianc

e

'wild

card

s' a

ppea

r on

type

def

initi

ons

Use

site

s m

uch

sim

pler

Can

ret

rofit

mos

t AP

Isbu

t not

Col

lect

ion

Page 12: Neal Gafter Java Evolution

Au

tob

oxi

ng

, un

bo

xin

g

Type

sys

tem

s fo

rm a

latti

ce

Obj

ect

/

\

Num

ber

R

unna

ble

|

/In

tege

r

/

\

/

n

ull

Page 13: Neal Gafter Java Evolution

Au

tob

oxi

ng

, un

bo

xin

g

Add

ing

conv

ersi

ons

can

brea

k th

e la

ttice

long

<--

----

----

> L

ong

|

|

|

|

int <

----

----

-->

Inte

ger

Exi

stin

g bo

xed

type

s do

n't h

ave

this

rel

atio

n.

Page 14: Neal Gafter Java Evolution

Au

tob

oxi

ng

, un

bo

xin

g

Sol

utio

ns

Intr

oduc

e ne

w b

oxin

g in

terf

aces

; or

Pat

chw

ork

spec

ifica

tion

Page 15: Neal Gafter Java Evolution

for-

each

(vs

iter

ato

r)

Itera

tor

has

a ve

stig

al r

emov

e m

etho

d.In

trod

uce

java

.lang

.Iter

ator

with

out i

t?C

anno

t ret

rofit

Col

lect

ion

with

out r

equi

ring

reco

mpi

le

(exi

stin

g im

plem

enta

tions

don

't im

plem

ent

itera

tor(

) th

at r

etur

ns th

e ne

w ty

pe)

Page 16: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Page 17: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Enc

oura

ge d

esira

ble

prac

tices

(th

at m

ight

no

t oth

erw

ise

be fo

llow

ed)

my

mak

ing

them

ea

sy sync

hron

ized

Ann

otat

ions

isol

ate

conf

igur

atio

n da

taG

ener

ics

for

type

safe

AP

Is

Page 18: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Enc

oura

ge d

esira

ble

prac

tices

Isol

ate

lang

uage

from

spe

cific

AP

Is

Page 19: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Enc

oura

ge d

esira

ble

prac

tices

Isol

ate

lang

uage

from

spe

cific

AP

IsP

refe

r re

adin

g ov

er w

ritin

gcl

arity

ove

r co

ncis

enes

s

Page 20: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Enc

oura

ge d

esira

ble

prac

tices

Isol

ate

lang

uage

from

spe

cific

AP

IsP

refe

r re

adin

g ov

er w

ritin

gP

refe

r st

atic

typi

ng

Page 21: Neal Gafter Java Evolution

Des

ign

Pri

nci

ple

s

Enc

oura

ge d

esira

ble

prac

tices

Isol

ate

lang

uage

from

spe

cific

AP

IsP

refe

r re

adin

g ov

er w

ritin

gP

refe

r st

atic

typi

ngR

emai

n ba

ckw

ard

com

patib

le

Page 22: Neal Gafter Java Evolution

Sh

ort

-ter

m d

esig

n G

oal

s

Reg

ular

ize

Exi

stin

g La

ngua

geIm

prov

e di

agno

stic

s vs

gen

eric

sF

ix ty

pe in

fere

nce

Str

ing

switc

hLi

mite

d op

erat

or o

verlo

adin

gIm

prov

ed c

atch

cla

uses

Mod

ular

ity

Page 23: Neal Gafter Java Evolution

Imp

rove

Dia

gn

ost

ics

vs G

ener

ics

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: Neal Gafter Java Evolution

Imp

rove

Dia

gn

ost

ics

vs G

ener

ics

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: Neal Gafter Java Evolution

Fix

Typ

e In

fere

nce

: co

nst

ruct

ors

Toda

y:

Map<String, List<String>> anagrams

= new HashMap<String,

List<String>>();

Page 26: Neal Gafter Java Evolution

Fix

Typ

e In

fere

nce

: co

nst

ruct

ors

Pro

pose

d:

Map<String, List<String>> anagrams

= new HashMap<>();

Page 27: Neal Gafter Java Evolution

Fix

Typ

e In

fere

nce

: ar

gu

men

ts

Toda

y:

publ

ic <E> Set<

E> emptySet

() { … }

void

timeWaitsF

or(Set<Man>

people)

{ … }

// *

Won't comp

ile!

time

WaitsFor(Co

llections.e

mptySet(

));

Page 28: Neal Gafter Java Evolution

Fix

Typ

e In

fere

nce

: ar

gu

men

ts

Toda

y:

publ

ic <E> Set<

E> emptySet

() { … }

void

timeWaitsF

or(Set<Man>

people)

{ … }

// O

Kti

meWaitsFor(Co

llections.<

Man>empt

ySet());

Page 29: Neal Gafter Java Evolution

Fix

Typ

e In

fere

nce

: ar

gu

men

ts

Pro

pose

d:

publ

ic <E> Set<

E> emptySet

() { … }

void

timeWaitsF

or(Set<Man>

people)

{ … }

// O

Kti

meWaitsFor(Co

llections.e

mptySet(

));

Page 30: Neal Gafter Java Evolution

Str

ing

Sw

itch

Toda

y

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: Neal Gafter Java Evolution

Str

ing

Sw

itch

Proposed

stat

ic boolean b

oolea

nFromStri

ng(String s) {

swit

ch(

s) {

ca

se

"true

":

retur

n t

rue;

ca

se

"fals

e":

retur

n f

alse;

de

fau

lt:

throw

ne

w Illega

lArgumentException(s);

}

}

Page 32: Neal Gafter Java Evolution

Lim

ited

Op

erat

or

Ove

rlo

adin

g

Toda

y

enum

Size

{ SMALL

, MED

IUM,

LARGE }

if (

mySiz

e.compar

eTo(y

ourSi

ze) >= 0

)

Syste

m.out.pr

intln

(“You

can wea

r my

pants

.”);

Page 33: Neal Gafter Java Evolution

Lim

ited

Op

erat

or

Ove

rlo

adin

g

Prop

osed

enum

Size

{ SMALL

, MED

IUM,

LARGE }

if (

mySiz

e > your

Size)

Syste

m.out.pr

intln

(“You

can wea

r my

pants

.”);

Page 34: Neal Gafter Java Evolution

Imp

rove

d C

atch

Cla

use

s: m

ult

i

Toda

y:

try {

return klass.newInstance();

} catch (InstantiationException e) {

throw new AssertionError(e);

} catch (IllegalAccessException e) {

throw new AssertionError(e);

}

Page 35: Neal Gafter Java Evolution

Imp

rove

d C

atch

Cla

use

s: m

ult

i

Pro

pose

d:

try {

return klass.newInstance();

} catch (InstantiationException | IllegalAccessException e) {

throw new AssertionError(e);

}

Page 36: Neal Gafter Java Evolution

Imp

rove

d C

atch

Cla

use

s: r

eth

row

Toda

y:

try

{

doable.

doit();

// Th

rows

several

types

} ca

tch (

Throwabl

e ex)

{

logger.

log(ex);

th

row e

x; //

Error:

Throw

able

not decl

ared

}

Page 37: Neal Gafter Java Evolution

Imp

rove

d C

atch

Cla

use

s: r

eth

row

Pro

pose

d:

try

{

doable.

doit();

// Th

rows

several

types

} ca

tch (

final

Throwab

le ex

) {

lo

gger.

log(ex);

th

row e

x; //

OK: Thr

ows t

he sa

me sever

al ty

pes

}

Page 38: Neal Gafter Java Evolution

Oth

ers?

Pro

pert

ies?

Ser

ializ

atio

n an

nota

tions

?

Page 39: Neal Gafter Java Evolution

Lo

ng

-ter

m g

oal

s

Reg

ular

ize

Exi

stin

g La

ngua

geR

eific

atio

nF

urth

er G

ener

ics

sim

plifi

catio

n

Con

curr

ency

sup

port

Imm

utab

le d

ata

Con

trol

Abs

trac

tion

Clo

sure

sA

ctor

s, e

tc.

Page 40: Neal Gafter Java Evolution

JDK

7

jsrs

277

+ 2

94 (

mod

ular

ity)

Mai

nten

ance

rev

iew

of j

sr14

“Sm

all”

lang

uage

issu

esP

ossi

bly

jsr3

08

(lim

ited

by ti

me,

res

ourc

es)

Page 41: Neal Gafter Java Evolution

JDK

8

Rei

ficat

ion

Con

trol

Abs

trac

tion

Fur

ther

out

: Im

mut

able

dat

a, p

atte

rn m

atch

-in

g, fu

rthe

r op

erat

or o

verlo

adin

g?

Page 42: Neal Gafter Java Evolution

Q&

A