Neal Gafter Java Evolution
Transcript of Neal Gafter Java Evolution
Evo
lvin
gth
eJa
va P
rog
ram
min
g L
ang
uag
e
Nea
l Gaf
ter
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
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
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
.
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.
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
.
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
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
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)
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
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
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
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.
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
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)
Des
ign
Pri
nci
ple
s
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
Des
ign
Pri
nci
ple
s
Enc
oura
ge d
esira
ble
prac
tices
Isol
ate
lang
uage
from
spe
cific
AP
Is
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
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
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
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
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());
^
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());
^
Fix
Typ
e In
fere
nce
: co
nst
ruct
ors
Toda
y:
Map<String, List<String>> anagrams
= new HashMap<String,
List<String>>();
Fix
Typ
e In
fere
nce
: co
nst
ruct
ors
Pro
pose
d:
Map<String, List<String>> anagrams
= new HashMap<>();
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(
));
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());
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(
));
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);
}
}
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);
}
}
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
.”);
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
.”);
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);
}
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);
}
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
}
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
}
Oth
ers?
Pro
pert
ies?
Ser
ializ
atio
n an
nota
tions
?
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.
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)
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?
Q&
A