SlideShare a Scribd company logo
#JFall #PatternMatching @hannotify @PeterWessels
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Pattern
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Matching
Small Enhancement or Major
Small Enhancement or Major
Feature?
Feature?
Hanno Embregts @hannotify
#JFall #PatternMatching @hannotify @PeterWessels
#JFall #PatternMatching @hannotify @PeterWessels
#JFall #PatternMatching @hannotify @PeterWessels
Small Enhancement
Small Enhancement
Small Enhancement
Small Enhancement
Small Enhancement
https://gph.is/g/ZPJNoPQ
#JFall #PatternMatching @hannotify @PeterWessels
Major Feature
Major Feature
Major Feature
Major Feature
Major Feature
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
Pattern
Pattern
Pattern
Pattern
Pattern
Matching for
Matching for
Matching for
Matching for
Matching for
instanceof
instanceof
instanceof
instanceof
instanceof
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
https://pxhere.com/en/photo/548063
#JFall #PatternMatching @hannotify @PeterWessels
Instanceof-and-cast
Instanceof-and-cast
if (product instanceof Guitar) {
Guitar lesPaul =
(Guitar) product;
// use lesPaul
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
Instanceof-and-cast
Instanceof-and-cast
if (product instanceof Guitar) { // 1. is product a Guitar?
1
Guitar lesPaul =
2
(Guitar) product;
3
// use lesPaul
4
}
5
#JFall #PatternMatching @hannotify @PeterWessels
Instanceof-and-cast
Instanceof-and-cast
(Guitar) product; // 2. perform conversion
if (product instanceof Guitar) { // 1. is product a Guitar?
1
Guitar lesPaul =
2
3
// use lesPaul
4
}
5
#JFall #PatternMatching @hannotify @PeterWessels
Instanceof-and-cast
Instanceof-and-cast
Guitar lesPaul = // 3. declare variable, bind value
if (product instanceof Guitar) { // 1. is product a Guitar?
1
2
(Guitar) product; // 2. perform conversion
3
// use lesPaul
4
}
5
#JFall #PatternMatching @hannotify @PeterWessels
Improve the situation
Improve the situation
if (product instanceof Guitar) { // 1. is product a Guitar?
Guitar lesPaul = // 3. declare variable, bind value
(Guitar) product; // 2. perform conversion
// use lesPaul
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
Improve the situation
Improve the situation
if (product instanceof Guitar lesPaul) {
1
2 // use lesPaul
3 }
#JFall #PatternMatching @hannotify @PeterWessels
Type pattern
Type pattern
Type pattern
Type pattern
Type pattern
Consists of a predicate that specifies a
type, along with a single binding variable.
https://www.pexels.com/photo/person-holding-white-chalk-625219/
#JFall #PatternMatching @hannotify @PeterWessels
Pattern matching
Pattern matching
Pattern matching
Pattern matching
Pattern matching
Allows the conditional extraction of
components from objects to be
expressed more concisely and safely.
https://www.pexels.com/photo/person-holding-white-chalk-625219/
#JFall #PatternMatching @hannotify @PeterWessels
Demo
Demo
Simplify implementation of equals
https://pxhere.com/en/photo/1458897
#JFall #PatternMatching @hannotify @PeterWessels
Declaring 'in the middle'
Declaring 'in the middle'
if (product instanceof Guitar lesPaul) {
// use lesPaul
}
1
2
3
#JFall #PatternMatching @hannotify @PeterWessels
Scoping
Scoping
Pattern binding variable ('flow scoping')
Pattern binding variable ('flow scoping')
The set of places where it would definitely be assigned.
if (product instanceof Guitar lesPaul && lesPaul.isInTune()) {
// can use lesPaul here
} else {
// can't use lesPaul here
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
pattern example
type pattern Guitar lesPaul
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Type Patterns
Type Patterns
Java version Feature status JEP
14 Preview
15 Second preview
16 Final
JEP 305
JEP 375
JEP 394
#JFall #PatternMatching @hannotify @PeterWessels
Pattern
Pattern
Pattern
Pattern
Pattern
Matching for
Matching for
Matching for
Matching for
Matching for
switch
switch
switch
switch
switch https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
https://pxhere.com/en/photo/548063
#JFall #PatternMatching @hannotify @PeterWessels
https://pxhere.com/en/photo/544037
#JFall #PatternMatching @hannotify @PeterWessels
https://pxhere.com/en/photo/544037
#JFall #PatternMatching @hannotify @PeterWessels
String apply(Effect effect) {
String formatted = "";
if (effect instanceof Delay) {
Delay de = (Delay) effect;
formatted = String.format("Delay active of %d ms.", de.getTimeInMs()
} else if (effect instanceof Reverb) {
Reverb re = (Reverb) effect;
formatted = String.format("Reverb active of type %s and roomSize %d.
} else if (effect instanceof Overdrive) {
Overdrive ov = (Overdrive) effect;
formatted = String.format("Overdrive active with gain %d.", ov.getGa
} else if (effect instanceof Tremolo) {
Tremolo tr = (Tremolo) effect;
formatted = String.format("Tremolo active with depth %d and rate %d.
} else if (effect instanceof Tuner) {
Tuner tu (Tuner) effect;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#JFall #PatternMatching @hannotify @PeterWessels
formatted String.format( Reverb active of type %s and roomSize %d.
formatted = String.format("Overdrive active with gain %d.", ov.getGa
formatted = String.format("Tremolo active with depth %d and rate %d.
formatted = String.format("Tuner active with pitch %d. Muting all si
formatted = el.getEffects().stream().map(this::apply).collect(Collec
formatted = String.format("Unknown effect active: %s.", effect);
8
} else if (effect instanceof Overdrive) {
9
Overdrive ov = (Overdrive) effect;
10
11
} else if (effect instanceof Tremolo) {
12
Tremolo tr = (Tremolo) effect;
13
14
} else if (effect instanceof Tuner) {
15
Tuner tu = (Tuner) effect;
16
17
} else if (effect instanceof EffectLoop) {
18
EffectLoop el = (EffectLoop) effect;
19
20
} else {
21
22
}
23
#JFall #PatternMatching @hannotify @PeterWessels
String apply(Effect effect) {
String formatted = "";
if (effect instanceof Delay de) {
} else if (effect instanceof Reverb re) {
} else if (effect instanceof Overdrive ov) {
} else if (effect instanceof Tremolo tr) {
} else if (effect instanceof Tuner tu) {
} else if (effect instanceof EffectLoop el) {
1
2
3
4 formatted = String.format("Delay active of %d ms.", de.getTimeInMs()
5
6 formatted = String.format("Reverb active of type %s and roomSize %d.
7
8 formatted = String.format("Overdrive active with gain %d.", ov.getGa
9
10 formatted = String.format("Tremolo active with depth %d and rate %d.
11
12 formatted = String.format("Tuner active with pitch %d. Muting all si
13
14 formatted = el.getEffects().stream().map(this::apply).collect(Collec
15 } else {
16 formatted String format("Unknown effect active: %s " effect);
#JFall #PatternMatching @hannotify @PeterWessels
formatted = String.format( Delay active of %d ms. , de.getTimeInMs()
formatted = String.format("Reverb active of type %s and roomSize %d.
formatted = String.format("Overdrive active with gain %d.", ov.getGa
formatted = String.format("Tremolo active with depth %d and rate %d.
formatted = String.format("Tuner active with pitch %d. Muting all si
formatted = el.getEffects().stream().map(this::apply).collect(Collec
formatted = String.format("Unknown effect active: %s.", effect);
4
} else if (effect instanceof Reverb re) {
5
6
} else if (effect instanceof Overdrive ov) {
7
8
} else if (effect instanceof Tremolo tr) {
9
10
} else if (effect instanceof Tuner tu) {
11
12
} else if (effect instanceof EffectLoop el) {
13
14
} else {
15
16
}
17
return formatted;
18
}
19
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
1 String apply(Effect effect) {
2 return switch(effect) {
3 default -> String.format("Unknown effect active: %s.", eff
4 };
5 }
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
case Delay de -> String.format("Delay active of %d ms.", de.getT
case Reverb re -> String.format("Reverb active of type %s and roo
String apply(Effect effect) {
1
return switch(effect) {
2
3
4
5 default -> String.format("Unknown effect active: %s.", eff
6 };
7 }
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
1 String apply(Effect effect) {
2 return switch(effect) {
3 case Delay de -> String.format("Delay active of %d ms.", de.get
4 case Reverb re -> String.format("Reverb active of type %s and ro
5 case Overdrive ov -> String.format("Overdrive active with gain %d."
6 case Tremolo tr -> String.format("Tremolo active with depth %d an
7 case Tuner tu -> String.format("Tuner active with pitch %d. Mut
8 default -> String.format("Unknown effect active: %s.", ef
9 };
10 }
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
String apply(Effect effect) {
1
return switch(effect) {
2
case Delay de -> String.format("Delay active of %d ms.", de.get
3
case Reverb re -> String.format("Reverb active of type %s and ro
4
case Overdrive ov -> String.format("Overdrive active with gain %d."
5
case Tremolo tr -> String.format("Tremolo active with depth %d an
6
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
7
8
9 default -> String.format("Unknown effect active: %s.", ef
10 };
11 }
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
String apply(Effect effect) {
return switch(effect) {
case Delay de -> String.format("Delay active of %d ms.", de.get
case Reverb re -> String.format("Reverb active of type %s and ro
case Overdrive ov -> String.format("Overdrive active with gain %d."
case Tremolo tr -> String.format("Tremolo active with depth %d an
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
default -> String.format("Unknown effect active: %s.", ef
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
https://pxhere.com/en/photo/544037
#JFall #PatternMatching @hannotify @PeterWessels
Sensible operations to the effect
Sensible operations to the effect
loop
loop
apply()
setVolume(int volume)
contains(Effect... effect)
#JFall #PatternMatching @hannotify @PeterWessels
Nonsensical operations to the
Nonsensical operations to the
effect loop
effect loop
isTunerActive()
isDelayTimeEqualToReverbRoomSize()
isToneSuitableToPlayPrideInTheNameOfLove()
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
String apply(Effect effect) {
return switch(effect) {
case Delay de -> String.format("Delay active of %d ms.", de.get
case Reverb re -> String.format("Reverb active of type %s and ro
case Overdrive ov -> String.format("Overdrive active with gain %d."
case Tremolo tr -> String.format("Tremolo active with depth %d an
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
default -> String.format("Unknown effect active: %s.", ef
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
Switch expression
Switch expression
static String apply(Effect effect) {
return switch(effect) {
case Delay de -> String.format("Delay active of %d ms.", de.get
case Reverb re -> String.format("Reverb active of type %s and ro
case Overdrive ov -> String.format("Overdrive active with gain %d."
case Tremolo tr -> String.format("Tremolo active with depth %d an
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co
default -> String.format("Unknown effect active: %s.", ef
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
Benefits of pattern
Benefits of pattern
matching
matching
No need for the Visitor pattern or a common supertype
A single expression instead of many assignments
Less error-prone (in adding cases)
More concise
Safer - the compiler can check for missing cases
#JFall #PatternMatching @hannotify @PeterWessels
But what if
But what if effect
effect is
is null
null?
?
return switch(effect) { // throws NullPointerException!
static String apply(Effect effect) {
1
2
case Delay de -> String.format("Delay active of %d ms.", de.get
3
case Reverb re -> String.format("Reverb active of type %s and ro
4
case Overdrive ov -> String.format("Overdrive active with gain %d."
5
case Tremolo tr -> String.format("Tremolo active with depth %d an
6
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
7
case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co
8
default -> String.format("Unknown effect active: %s.", ef
9
};
10
}
11
#JFall #PatternMatching @hannotify @PeterWessels
Combining case labels
Combining case labels
case null, default -> String.format("Unknown or malfunctioning effec
static String apply(Effect effect) {
1
return switch(effect) {
2
case Delay de -> String.format("Delay active of %d ms.", de.get
3
case Reverb re -> String.format("Reverb active of type %s and ro
4
case Overdrive ov -> String.format("Overdrive active with gain %d."
5
case Tremolo tr -> String.format("Tremolo active with depth %d an
6
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
7
case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co
8
9
};
10
}
11
#JFall #PatternMatching @hannotify @PeterWessels
Demo
Demo
Guarded patterns
https://pxhere.com/en/photo/1458897
#JFall #PatternMatching @hannotify @PeterWessels
Guarded patterns
Guarded patterns
String apply(Effect effect, Guitar guitar) {
return switch(effect) {
// (...)
case Tremolo tr -> String.format("Tremolo active with depth %d and ra
case Tuner tu -> String.format("Tuner active with pitch %d. Muting al
case EffectLoop el -> el.getEffects().stream().map(this::apply).colle
default -> String.format("Unknown effect active: %s.", effect);
};
}
1
2
3
4
5
6
7
8
9
#JFall #PatternMatching @hannotify @PeterWessels
Guarded patterns
Guarded patterns
case Tuner tu && !tu.isInTune(guitar) -> String.format("Guitar is in
String apply(Effect effect, Guitar guitar) {
1
return switch(effect) {
2
// (...)
3
case Tremolo tr -> String.format("Tremolo active with depth %d and ra
4
5
case EffectLoop el -> el.getEffects().stream().map(this::apply).colle
6
default -> String.format("Unknown effect active: %s.", effect);
7
};
8
}
9
#JFall #PatternMatching @hannotify @PeterWessels
Guarded patterns
Guarded patterns
case Tuner tu when !tu.isInTune(guitar) -> String.format("Guitar is i
String apply(Effect effect, Guitar guitar) {
1
return switch(effect) {
2
// (...)
3
case Tremolo tr -> String.format("Tremolo active with depth %d and ra
4
5
case EffectLoop el -> el.getEffects().stream().map(this::apply).colle
6
default -> String.format("Unknown effect active: %s.", effect);
7
};
8
}
9
#JFall #PatternMatching @hannotify @PeterWessels
Guarded patterns
Guarded patterns
switch(effect) {
// ...
case Tuner tu:
if (!tu.isInTune(guitar)) { // tuning is needed }
else { // no tuning is needed }
break;
// ...
}
1
2
3
4
5
6
7
8
#JFall #PatternMatching @hannotify @PeterWessels
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
pattern example
guarded pattern Tuner tu when
!tu.isInTune(guitar)
type pattern Guitar lesPaul
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Pattern Matching for Switch
Pattern Matching for Switch
Java version Feature status JEP
17 Preview
18 Second preview
19 Third preview
20 Fourth preview
JEP 406
JEP 420
JEP 427
JEP 433
#JFall #PatternMatching @hannotify @PeterWessels
Deconstructio
Deconstructio
Deconstructio
Deconstructio
Deconstructio
n Patterns
n Patterns
n Patterns
n Patterns
n Patterns
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Disclaimer
Disclaimer
Disclaimer
Disclaimer
Disclaimer
We can't tell you when the following
features are coming to Java. Also: syntax
and implementation specifics may still
change.
https://pxhere.com/en/photo/1359311
#JFall #PatternMatching @hannotify @PeterWessels
Deconstruction patterns
Deconstruction patterns
String apply(Effect effect) {
return switch(effect) {
case Delay de -> String.format("Delay active of %d ms.", de.get
case Reverb re -> String.format("Reverb active of type %s and ro
case Overdrive ov -> String.format("Overdrive active with gain %d."
case Tremolo tr -> String.format("Tremolo active with depth %d an
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
default -> String.format("Unknown effect active: %s.", ef
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
Deconstruction patterns
Deconstruction patterns
case Overdrive(int gain) -> String.format("Overdrive active with gai
String apply(Effect effect) {
1
return switch(effect) {
2
case Delay de -> String.format("Delay active of %d ms.", de.get
3
case Reverb re -> String.format("Reverb active of type %s and ro
4
5
case Tremolo tr -> String.format("Tremolo active with depth %d an
6
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
7
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
8
default -> String.format("Unknown effect active: %s.", ef
9
};
10
}
11
#JFall #PatternMatching @hannotify @PeterWessels
Pattern definition
Pattern definition
public class Overdrive implements Effect {
private final int gain;
public Overdrive(int gain) {
this.gain = gain;
}
}
1
2
3
4
5
6
7
#JFall #PatternMatching @hannotify @PeterWessels
Pattern definition
Pattern definition
1 public class Overdrive implements Effect {
2 private final int gain;
3
4 public Overdrive(int gain) {
5 this.gain = gain;
6 }
7
public pattern Overdrive(int gain) {
8
gain = this.gain;
9
}
10
11 }
#JFall #PatternMatching @hannotify @PeterWessels
Deconstruction patterns
Deconstruction patterns
case Overdrive(int gain) -> String.format("Overdrive active with gai
String apply(Effect effect) {
1
return switch(effect) {
2
case Delay de -> String.format("Delay active of %d ms.", de.get
3
case Reverb re -> String.format("Reverb active of type %s and ro
4
5
case Tremolo tr -> String.format("Tremolo active with depth %d an
6
case Tuner tu -> String.format("Tuner active with pitch %d. Mut
7
case EffectLoop el -> el.getEffects().stream().map(this::apply).coll
8
default -> String.format("Unknown effect active: %s.", ef
9
};
10
}
11
#JFall #PatternMatching @hannotify @PeterWessels
Demo
Demo
Deconstruction with Records
https://pxhere.com/en/photo/1458897
#JFall #PatternMatching @hannotify @PeterWessels
Deconstruction patterns
Deconstruction patterns
String apply(Effect effect) {
return switch(effect) {
case Delay(int timeInMs) -> String.format("Delay active of %d ms.",
case Reverb(String name, int roomSize) -> String.format("Reverb acti
case Overdrive(int gain) -> String.format("Overdrive active with gai
case Tremolo(int depth, int rate) -> String.format("Tremolo active w
case Tuner(int pitchInHz) -> String.format("Tuner active with pitch
case EffectLoop(Set<Effect> effects) -> effects.stream().map(this::a
default -> String.format("Unknown effect active: %s.", effect);
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
Can we combine patterns?
Can we combine patterns?
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
}
1
2
3
#JFall #PatternMatching @hannotify @PeterWessels
Pattern composition
Pattern composition
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
if (effectLoop instanceof EffectLoop(Delay(int timeInMs), Reverb(String n
return timeInMs == roomSize;
1
2
3
}
4
return false;
5
6 }
#JFall #PatternMatching @hannotify @PeterWessels
Pattern composition
Pattern composition
1 static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
2 return effectLoop.getEffects().stream()
3 .filter(e -> e instanceof Delay || e instanceof Reverb)
4 .map(dr -> {
5 if (dr instanceof Delay d) {
6 return d.getTimeInMs();
} else {
7
Reverb r = (Reverb) dr;
8
return r.getRoomSize();
9
}
10
11
}).distinct().count() == 1;
12
13 }
#JFall #PatternMatching @hannotify @PeterWessels
Var patterns
Var patterns
// Pre-Java 10
Guitar telecaster = new Guitar("Fender Telecaster Baritone Blacktop", GuitarT
// Java 10
var telecaster = new Guitar("Fender Telecaster Baritone Blacktop", GuitarType
1
2
3
4
5
https://openjdk.java.net/jeps/286
https://openjdk.java.net/jeps/286
#JFall #PatternMatching @hannotify @PeterWessels
Var patterns
Var patterns
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
if (effectLoop instanceof EffectLoop(Delay(int timeInMs), Reverb(String n
return timeInMs == roomSize;
}
return false;
}
1
2
3
4
5
6
#JFall #PatternMatching @hannotify @PeterWessels
Var patterns
Var patterns
if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(var name
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
1
2
return timeInMs == roomSize;
3
}
4
return false;
5
}
6
#JFall #PatternMatching @hannotify @PeterWessels
Benefits of
Benefits of
deconstruction patterns
deconstruction patterns
Better encapsulation
a case branch only receives data that it actually references.
More elegant logic
by using pattern composition
#JFall #PatternMatching @hannotify @PeterWessels
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
pattern example
deconstruction pattern Delay(int timeInMs)
type pattern Guitar lesPaul
guarded pattern Tuner tu when
!tu.isInTune(guitar)
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
pattern example
var pattern var timeInMs
type pattern Guitar lesPaul
guarded pattern Tuner tu when
!tu.isInTune(guitar)
deconstruction pattern Delay(int timeInMs)
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Deconstruction patterns
Deconstruction patterns
Java
version
Feature status JEP
19 Preview (deconstruction with record
patterns only)
n/a Second preview (deconstruction with
record patterns only)
JEP
405
JEP
432
#JFall #PatternMatching @hannotify @PeterWessels
Pattern Matching Plays
Pattern Matching Plays
Nice With
Nice With
Sealed Types
Sealed Types
Sealed Types
Sealed Types
Sealed Types
and Records
and Records
and Records
and Records
and Records
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Demo
Demo
Make Effect a sealed type
https://pxhere.com/en/photo/1458897
#JFall #PatternMatching @hannotify @PeterWessels
Sealed types yield completeness
Sealed types yield completeness
static String apply(Effect effect) {
return switch(effect) {
case Delay(int timeInMs) -> String.format("Delay active of %d ms.",
case Reverb(String name, int roomSize) -> String.format("Reverb acti
case Overdrive(int gain) -> String.format("Overdrive active with gai
case Tremolo(int depth, int rate) -> String.format("Tremolo active w
case Tuner(int pitchInHz) -> String.format("Tuner active with pitch
case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effec
case EffectLoop(var effects) -> effects.stream().map(Effect::apply).
default -> String.format("Unknown effect active: %s.", effect);
};
}
1
2
3
4
5
6
7
8
9
10
11
12
#JFall #PatternMatching @hannotify @PeterWessels
Sealed types yield completeness
Sealed types yield completeness
static String apply(Effect effect) {
return switch(effect) {
case Delay(int timeInMs) -> String.format("Delay active of %d ms.",
case Reverb(String name, int roomSize) -> String.format("Reverb acti
case Overdrive(int gain) -> String.format("Overdrive active with gai
case Tremolo(int depth, int rate) -> String.format("Tremolo active w
case Tuner(int pitchInHz) -> String.format("Tuner active with pitch
case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effec
case EffectLoop(var effects) -> effects.stream().map(Effect::apply).
1
2
3
4
5
6
7
8
9
10 };
11 }
https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java
#JFall #PatternMatching @hannotify @PeterWessels
Records
Records
Input:
Input:
Commit to the class being a transparent carrier for its
data.
Output:
Output:
#JFall #PatternMatching @hannotify @PeterWessels
Patterns in enhanced for
Patterns in enhanced for
record Delay(int timeInMs) implements Effect {}
1
static void printDelays(List<Delay> delays) {
for (Delay delay : delays) {
System.out.println("Delay found with timeInMs=" + delay.timeInMs());
}
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
Patterns in enhanced for
Patterns in enhanced for
record Delay(int timeInMs) implements Effect {}
1
static void printDelays(List<Delay> delays) {
for (Delay(int timeInMs) : delays) {
System.out.println("Delay found with timeInMs=" + timeInMs);
}
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Sealed Types
Sealed Types
Java version Feature status JEP
15 Preview
16 Second preview
17 Final
JEP 360
JEP 397
JEP 409
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Completeness
Completeness
Java version Feature status JEP
17 Preview
18 Second preview
19 Third preview
JEP 406
JEP 420
JEP 427
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Record Patterns
Record Patterns
Java version Feature status JEP
19 Preview
n/a Second preview
JEP 405
JEP 432
#JFall #PatternMatching @hannotify @PeterWessels
A Better
A Better
A Better
A Better
A Better
Serialization?
Serialization?
Serialization?
Serialization?
Serialization?
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Here be dragons!
Here be dragons!
Here be dragons!
Here be dragons!
Here be dragons!
We can't be sure at all that the following
features will appear in Java as depicted.
They can change a lot in the meantime.
https://www.pexels.com/photo/dragon-festival-during-nighttime-6068535/
#JFall #PatternMatching @hannotify @PeterWessels
Opposites
Opposites
Deconstruction pattern
Deconstruction pattern
transforms an object into a set of typed fields
Constructor
Constructor
transforms a set of typed fields into an object
#JFall #PatternMatching @hannotify @PeterWessels
Serialization
Serialization
very important feature
but many people hate its current implementation
Drawbacks
Drawbacks
it undermines the accessibility model
serialization logic is not 'readable code'
it bypasses constructors and data validation
#JFall #PatternMatching @hannotify @PeterWessels
Serialization
Serialization
public class EffectLoop implements Effect {
private String name;
private Set<Effect> effects;
public EffectLoop(String name) {
this.name = name;
this.effects = new HashSet<>();
}
}
1
2
3
4
5
6
7
8
9
#JFall #PatternMatching @hannotify @PeterWessels
Serialization
Serialization
1 public class EffectLoop implements Effect {
2 private String name;
3 private Set<Effect> effects;
4
5 public EffectLoop(String name) {
6 this.name = name;
7 this.effects = new HashSet<>();
8 }
9
public pattern EffectLoop(String name, Effect[] effects) {
10
name = this.name;
11
effects = this.effects.toArray();
12
}
13
14 }
#JFall #PatternMatching @hannotify @PeterWessels
Serialization
Serialization
public EffectLoop(String name, Effect[] effects) {
this(name);
for (Effect effect : effects) {
this.effects.add(effect);
}
this.effects = new HashSet<>();
7
}
8
9
10
11
12
13
14
15 }
16
17 public pattern EffectLoop(String name, Effect[] effects) {
18 name = this.name;
19 effects = this.effects.toArray();
}
20
21 }
#JFall #PatternMatching @hannotify @PeterWessels
Serialization
Serialization
public class EffectLoop implements Effect {
private String name;
private Set<Effect> effects;
public EffectLoop(String name) {
this.name = name;
this.effects = new HashSet<>();
}
@Deserializer
1
2
3
4
5
6
7
8
9
10
11 public EffectLoop(String name, Effect[] effects) {
12 this(name);
13 for (Effect effect : effects) {
14 this.effects.add(effect);
15 }
16 }
#JFall #PatternMatching @hannotify @PeterWessels
Some challenges remain
Some challenges remain
Q:
Q: How to support multiple versions of one class?
How to support multiple versions of one class?
A: @Serializer and @Deserializer annotations could
get a property version in the future.
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Java
version
Feature status JEP
n/a Exploratory
document
Towards Better
Serialization
https://cr.openjdk.java.net/~briangoetz/amber/serialization.html
#JFall #PatternMatching @hannotify @PeterWessels
Future
Future
Future
Future
Future
Expansions
Expansions
Expansions
Expansions
Expansions
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Unnamed patterns
Unnamed patterns
if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(var name
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
1
2
return timeInMs == roomSize;
3
}
4
return false;
5
}
6
#JFall #PatternMatching @hannotify @PeterWessels
Unnamed patterns
Unnamed patterns
static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) {
if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(_, var r
return timeInMs == roomSize;
}
return false;
}
1
2
3
4
5
6
#JFall #PatternMatching @hannotify @PeterWessels
Optimization
Optimization
static String apply(Effect effect) {
return switch(effect) {
case Delay(int timeInMs) -> String.format("Delay active of %d ms.",
case Reverb(String name, int roomSize) -> String.format("Reverb acti
case Overdrive(int gain) -> String.format("Overdrive active with gai
case Tremolo(int depth, int rate) -> String.format("Tremolo active w
case Tuner(int pitchInHz) -> String.format("Tuner active with pitch
case EffectLoop(var effects) -> effects.stream().map(Effect::apply).
default -> String.format("Unknown effect active: %s.", effect);
};
}
1
2
3
4
5
6
7
8
9
10
11
#JFall #PatternMatching @hannotify @PeterWessels
Optimization
Optimization
1 static String apply(Effect effect) {
2 return switch(effect) {
3 // ...
4 case EffectLoop(var effects) -> effects.stream().map(Effect::apply).c
5 default -> String.format("Unknown effect active: %s.", effect);
6 };
7 }
#JFall #PatternMatching @hannotify @PeterWessels
Optimization
Optimization
case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effect
static String apply(Effect effect) {
1
return switch(effect) {
2
// ...
3
4
5 case EffectLoop(var effects) -> effects.stream().map(Effect::apply).c
6 default -> String.format("Unknown effect active: %s.", effect);
7 };
8 }
#JFall #PatternMatching @hannotify @PeterWessels
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
It's a kind of Pattern
pattern example
unnamed pattern _
type pattern Guitar lesPaul
guarded pattern Tuner tu when
!tu.isInTune(guitar)
deconstruction pattern Delay(int timeInMs)
var pattern var timeInMs
https://thumbs.gfycat.com/DefiantElasticGadwall.webp
#JFall #PatternMatching @hannotify @PeterWessels
Feature Status
Feature Status
Unnamed Patterns
Unnamed Patterns
Java version Feature status JEP
n/a JEP draft 8294349
https://openjdk.org/jeps/8294349
https://openjdk.org/jeps/8294349
#JFall #PatternMatching @hannotify @PeterWessels
Here be dragons!
Here be dragons!
Here be dragons!
Here be dragons!
Here be dragons!
We can't be sure at all that the following
features will appear in Java as depicted.
They can change a lot in the meantime.
https://www.pexels.com/photo/dragon-festival-during-nighttime-6068535/
#JFall #PatternMatching @hannotify @PeterWessels
Pattern bind statements
Pattern bind statements
var reverb = new Reverb("ChamberReverb", 2);
__let Reverb(String name, int roomSize) = reverb;
// do something with name & roomSize
1
2
3
4
5
https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java
#JFall #PatternMatching @hannotify @PeterWessels
Pattern bind statements
Pattern bind statements
else throw new IllegalArgumentException("not a Reverb!");
var reverb = new Reverb("ChamberReverb", 2);
1
2
__let Reverb(String name, int roomSize) = reverb;
3
4
5
6 // do something with name & roomSize
#JFall #PatternMatching @hannotify @PeterWessels
Array patterns
Array patterns
record EffectLoop(String name, int volume, Effect... effects) { }
1
static String apply(EffectLoop effectLoop) {}
return switch(effectLoop) {
case EffectLoop(var name, var volume) -> "Effect loop contains no eff
}
}
1
2
3
4
5
#JFall #PatternMatching @hannotify @PeterWessels
Array patterns
Array patterns
record EffectLoop(String name, int volume, Effect... effects) { }
1
static String apply(EffectLoop effectLoop) {}
return switch(effectLoop) {
case EffectLoop(var name, var volume) -> "Effect loop contains no eff
case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on
1
2
3
4
5 }
6 }
#JFall #PatternMatching @hannotify @PeterWessels
Array patterns
Array patterns
record EffectLoop(String name, int volume, Effect... effects) { }
1
static String apply(EffectLoop effectLoop) {}
return switch(effectLoop) {
case EffectLoop(var name, var volume) -> "Effect loop contains no eff
case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on
case EffectLoop(_, _, var effect, ...) -> "Effect loop contains more
1
2
3
4
5
6 }
7 }
#JFall #PatternMatching @hannotify @PeterWessels
Array patterns
Array patterns
record EffectLoop(String name, int volume, Effect... effects) { }
1
static String apply(EffectLoop effectLoop) {}
return switch(effectLoop) {
case EffectLoop(var name, var volume) -> "Effect loop contains no eff
case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on
case EffectLoop(_, _, var effect, ...) -> "Effect loop contains more
case EffectLoop(_, _, var effect1, var effect2) -> "Effect loop conta
case EffectLoop(_, _, var effect1, var effect2, ...) -> "Effect loop
1
2
3
4
5
6
7
8 }
9 }
#JFall #PatternMatching @hannotify @PeterWessels
Other ideas
Other ideas
Deconstruction patterns for arbitrary classes
Enhanced array patterns
String[] { [8] -> var eighthElement, [9] -> var ninthElement}
AND patterns
Patterns in catch clauses
Collection patterns
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2021-January/002758.html
#JFall #PatternMatching @hannotify @PeterWessels
Pattern
Pattern
Pattern
Pattern
Pattern
Contexts
Contexts
Contexts
Contexts
Contexts
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Pattern Contexts
Pattern Contexts
Pattern
context
Example Purpose
instanceof
predicate
product instanceof
Guitar guitar
Test if target matches the
indicated pattern.
switch
statement or
expression
switch (effect) {
case Delay d ->
}
Test if target matches one (or
more) of the indicated patterns.
enhanced for for (Delay(int
timeInMs) : delays) {
}
Perform a iteration for each
target that matches the indicated
pattern.
bind
statement
__let Reverb(var
name, var roomSize) =
Destructure a target using a
pattern.
#JFall #PatternMatching @hannotify @PeterWessels
Wrap-up
Wrap-up
Wrap-up
Wrap-up
Wrap-up
https://pxhere.com/en/photo/752901
#JFall #PatternMatching @hannotify @PeterWessels
Pattern matching...
Pattern matching...
is a rich feature arc that will play out over several
versions.
allows us to use type patterns in instanceof.
improves switch expressions.
makes destructuring objects as easy as (and more similar
to) constructing them.
holds the potential to simplify and streamline much of
the code we write today.
#JFall #PatternMatching @hannotify @PeterWessels
Major Feature
Major Feature
Major Feature
Major Feature
Major Feature
#JFall #PatternMatching @hannotify @PeterWessels
Thank you! 😀
Thank you! 😀
github.com/hannotify/pattern-
matching-music-store
hanno.codes peterwessels.nl
@hannotify @PeterWessels

More Related Content

Similar to Pattern Matching: Small Enhancement or Major Feature?

7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)
Steven Francia
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
mussawir20
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017
Baruch Sadogursky
 
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner) Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
Puppet
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
Mike Fogus
 
F# Eye 4 the C# Guy
F# Eye 4 the C# GuyF# Eye 4 the C# Guy
F# Eye 4 the C# Guy
Phillip Trelford
 

Similar to Pattern Matching: Small Enhancement or Major Feature? (8)

7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)7 Common Mistakes in Go (2015)
7 Common Mistakes in Go (2015)
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017Java 8 Puzzlers as it was presented at Codemash 2017
Java 8 Puzzlers as it was presented at Codemash 2017
 
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner) Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
Puppet Camp Paris 2015: Power of Puppet 4 (Beginner)
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Naïveté vs. Experience
Naïveté vs. ExperienceNaïveté vs. Experience
Naïveté vs. Experience
 
F# Eye 4 the C# Guy
F# Eye 4 the C# GuyF# Eye 4 the C# Guy
F# Eye 4 the C# Guy
 

More from 🎤 Hanno Embregts 🎸

"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
🎤 Hanno Embregts 🎸
 
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
🎤 Hanno Embregts 🎸
 
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
🎤 Hanno Embregts 🎸
 
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
🎤 Hanno Embregts 🎸
 
Entering the Fourth Dimension of OCR with Tesseract
Entering the Fourth Dimension of OCR with TesseractEntering the Fourth Dimension of OCR with Tesseract
Entering the Fourth Dimension of OCR with Tesseract
🎤 Hanno Embregts 🎸
 
The Soft Side of Software Development / Devoxx 2019
The Soft Side of Software Development / Devoxx 2019The Soft Side of Software Development / Devoxx 2019
The Soft Side of Software Development / Devoxx 2019
🎤 Hanno Embregts 🎸
 
Beware of Survivorship Bias! (conference talk at J-Fall 2019)
Beware of Survivorship Bias! (conference talk at J-Fall 2019)Beware of Survivorship Bias! (conference talk at J-Fall 2019)
Beware of Survivorship Bias! (conference talk at J-Fall 2019)
🎤 Hanno Embregts 🎸
 
Will Git Be Around Forever? A List of Possible Successors
Will Git Be Around Forever? A List of Possible SuccessorsWill Git Be Around Forever? A List of Possible Successors
Will Git Be Around Forever? A List of Possible Successors
🎤 Hanno Embregts 🎸
 
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
🎤 Hanno Embregts 🎸
 
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
🎤 Hanno Embregts 🎸
 
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
🎤 Hanno Embregts 🎸
 
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
🎤 Hanno Embregts 🎸
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!
🎤 Hanno Embregts 🎸
 
QWERTY or DVORAK? Debunking the Keyboard Layout Myths
QWERTY or DVORAK? Debunking the Keyboard Layout MythsQWERTY or DVORAK? Debunking the Keyboard Layout Myths
QWERTY or DVORAK? Debunking the Keyboard Layout Myths
🎤 Hanno Embregts 🎸
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
🎤 Hanno Embregts 🎸
 
Migrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to GradleMigrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to Gradle
🎤 Hanno Embregts 🎸
 

More from 🎤 Hanno Embregts 🎸 (16)

"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
"Will Git Be Around Forever? A List of Possible Successors" from Devoxx 2022
 
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
"Will Git Be Around Forever? A List of Possible Successors" from FrontMania 2022
 
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
JCON 2021 talk - "Wil Git Be Around Forever? A List of Possible Successors"
 
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
"Will Git Be Around Forever? A List of Possible Successors" at UtrechtJUG
 
Entering the Fourth Dimension of OCR with Tesseract
Entering the Fourth Dimension of OCR with TesseractEntering the Fourth Dimension of OCR with Tesseract
Entering the Fourth Dimension of OCR with Tesseract
 
The Soft Side of Software Development / Devoxx 2019
The Soft Side of Software Development / Devoxx 2019The Soft Side of Software Development / Devoxx 2019
The Soft Side of Software Development / Devoxx 2019
 
Beware of Survivorship Bias! (conference talk at J-Fall 2019)
Beware of Survivorship Bias! (conference talk at J-Fall 2019)Beware of Survivorship Bias! (conference talk at J-Fall 2019)
Beware of Survivorship Bias! (conference talk at J-Fall 2019)
 
Will Git Be Around Forever? A List of Possible Successors
Will Git Be Around Forever? A List of Possible SuccessorsWill Git Be Around Forever? A List of Possible Successors
Will Git Be Around Forever? A List of Possible Successors
 
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
Entering the Fourth Dimension of OCR with Tesseract - Talk from Voxxed Days B...
 
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
QWERTY or DVORAK? Debunking the Keyboard Layout Myths -- from GeeCON 2018
 
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
Building a Spring Boot 2 Application - Ask the Audience! (from Voxxed Days Vi...
 
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
 
Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!Building a Spring Boot Application - Ask the Audience!
Building a Spring Boot Application - Ask the Audience!
 
QWERTY or DVORAK? Debunking the Keyboard Layout Myths
QWERTY or DVORAK? Debunking the Keyboard Layout MythsQWERTY or DVORAK? Debunking the Keyboard Layout Myths
QWERTY or DVORAK? Debunking the Keyboard Layout Myths
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
 
Migrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to GradleMigrating 25K lines of Ant scripting to Gradle
Migrating 25K lines of Ant scripting to Gradle
 

Recently uploaded

The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
OnePlan Solutions
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
seospiralmantra
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
alowpalsadig
 

Recently uploaded (20)

The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
 

Pattern Matching: Small Enhancement or Major Feature?

  • 1. #JFall #PatternMatching @hannotify @PeterWessels Pattern Pattern Pattern Pattern Pattern Pattern Pattern Pattern Pattern Pattern Pattern Pattern Matching Matching Matching Matching Matching Matching Matching Matching Matching Matching Matching Matching Small Enhancement or Major Small Enhancement or Major Feature? Feature? Hanno Embregts @hannotify
  • 4. #JFall #PatternMatching @hannotify @PeterWessels Small Enhancement Small Enhancement Small Enhancement Small Enhancement Small Enhancement https://gph.is/g/ZPJNoPQ
  • 5. #JFall #PatternMatching @hannotify @PeterWessels Major Feature Major Feature Major Feature Major Feature Major Feature https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 6. #JFall #PatternMatching @hannotify @PeterWessels Pattern Pattern Pattern Pattern Pattern Matching for Matching for Matching for Matching for Matching for instanceof instanceof instanceof instanceof instanceof https://pxhere.com/en/photo/752901
  • 7. #JFall #PatternMatching @hannotify @PeterWessels https://pxhere.com/en/photo/548063
  • 8. #JFall #PatternMatching @hannotify @PeterWessels Instanceof-and-cast Instanceof-and-cast if (product instanceof Guitar) { Guitar lesPaul = (Guitar) product; // use lesPaul } 1 2 3 4 5
  • 9. #JFall #PatternMatching @hannotify @PeterWessels Instanceof-and-cast Instanceof-and-cast if (product instanceof Guitar) { // 1. is product a Guitar? 1 Guitar lesPaul = 2 (Guitar) product; 3 // use lesPaul 4 } 5
  • 10. #JFall #PatternMatching @hannotify @PeterWessels Instanceof-and-cast Instanceof-and-cast (Guitar) product; // 2. perform conversion if (product instanceof Guitar) { // 1. is product a Guitar? 1 Guitar lesPaul = 2 3 // use lesPaul 4 } 5
  • 11. #JFall #PatternMatching @hannotify @PeterWessels Instanceof-and-cast Instanceof-and-cast Guitar lesPaul = // 3. declare variable, bind value if (product instanceof Guitar) { // 1. is product a Guitar? 1 2 (Guitar) product; // 2. perform conversion 3 // use lesPaul 4 } 5
  • 12. #JFall #PatternMatching @hannotify @PeterWessels Improve the situation Improve the situation if (product instanceof Guitar) { // 1. is product a Guitar? Guitar lesPaul = // 3. declare variable, bind value (Guitar) product; // 2. perform conversion // use lesPaul } 1 2 3 4 5
  • 13. #JFall #PatternMatching @hannotify @PeterWessels Improve the situation Improve the situation if (product instanceof Guitar lesPaul) { 1 2 // use lesPaul 3 }
  • 14. #JFall #PatternMatching @hannotify @PeterWessels Type pattern Type pattern Type pattern Type pattern Type pattern Consists of a predicate that specifies a type, along with a single binding variable. https://www.pexels.com/photo/person-holding-white-chalk-625219/
  • 15. #JFall #PatternMatching @hannotify @PeterWessels Pattern matching Pattern matching Pattern matching Pattern matching Pattern matching Allows the conditional extraction of components from objects to be expressed more concisely and safely. https://www.pexels.com/photo/person-holding-white-chalk-625219/
  • 16. #JFall #PatternMatching @hannotify @PeterWessels Demo Demo Simplify implementation of equals https://pxhere.com/en/photo/1458897
  • 17. #JFall #PatternMatching @hannotify @PeterWessels Declaring 'in the middle' Declaring 'in the middle' if (product instanceof Guitar lesPaul) { // use lesPaul } 1 2 3
  • 18. #JFall #PatternMatching @hannotify @PeterWessels Scoping Scoping Pattern binding variable ('flow scoping') Pattern binding variable ('flow scoping') The set of places where it would definitely be assigned. if (product instanceof Guitar lesPaul && lesPaul.isInTune()) { // can use lesPaul here } else { // can't use lesPaul here } 1 2 3 4 5
  • 19. #JFall #PatternMatching @hannotify @PeterWessels It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern pattern example type pattern Guitar lesPaul https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 20. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Type Patterns Type Patterns Java version Feature status JEP 14 Preview 15 Second preview 16 Final JEP 305 JEP 375 JEP 394
  • 21. #JFall #PatternMatching @hannotify @PeterWessels Pattern Pattern Pattern Pattern Pattern Matching for Matching for Matching for Matching for Matching for switch switch switch switch switch https://pxhere.com/en/photo/752901
  • 22. #JFall #PatternMatching @hannotify @PeterWessels https://pxhere.com/en/photo/548063
  • 23. #JFall #PatternMatching @hannotify @PeterWessels https://pxhere.com/en/photo/544037
  • 24. #JFall #PatternMatching @hannotify @PeterWessels https://pxhere.com/en/photo/544037
  • 25. #JFall #PatternMatching @hannotify @PeterWessels String apply(Effect effect) { String formatted = ""; if (effect instanceof Delay) { Delay de = (Delay) effect; formatted = String.format("Delay active of %d ms.", de.getTimeInMs() } else if (effect instanceof Reverb) { Reverb re = (Reverb) effect; formatted = String.format("Reverb active of type %s and roomSize %d. } else if (effect instanceof Overdrive) { Overdrive ov = (Overdrive) effect; formatted = String.format("Overdrive active with gain %d.", ov.getGa } else if (effect instanceof Tremolo) { Tremolo tr = (Tremolo) effect; formatted = String.format("Tremolo active with depth %d and rate %d. } else if (effect instanceof Tuner) { Tuner tu (Tuner) effect; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  • 26. #JFall #PatternMatching @hannotify @PeterWessels formatted String.format( Reverb active of type %s and roomSize %d. formatted = String.format("Overdrive active with gain %d.", ov.getGa formatted = String.format("Tremolo active with depth %d and rate %d. formatted = String.format("Tuner active with pitch %d. Muting all si formatted = el.getEffects().stream().map(this::apply).collect(Collec formatted = String.format("Unknown effect active: %s.", effect); 8 } else if (effect instanceof Overdrive) { 9 Overdrive ov = (Overdrive) effect; 10 11 } else if (effect instanceof Tremolo) { 12 Tremolo tr = (Tremolo) effect; 13 14 } else if (effect instanceof Tuner) { 15 Tuner tu = (Tuner) effect; 16 17 } else if (effect instanceof EffectLoop) { 18 EffectLoop el = (EffectLoop) effect; 19 20 } else { 21 22 } 23
  • 27. #JFall #PatternMatching @hannotify @PeterWessels String apply(Effect effect) { String formatted = ""; if (effect instanceof Delay de) { } else if (effect instanceof Reverb re) { } else if (effect instanceof Overdrive ov) { } else if (effect instanceof Tremolo tr) { } else if (effect instanceof Tuner tu) { } else if (effect instanceof EffectLoop el) { 1 2 3 4 formatted = String.format("Delay active of %d ms.", de.getTimeInMs() 5 6 formatted = String.format("Reverb active of type %s and roomSize %d. 7 8 formatted = String.format("Overdrive active with gain %d.", ov.getGa 9 10 formatted = String.format("Tremolo active with depth %d and rate %d. 11 12 formatted = String.format("Tuner active with pitch %d. Muting all si 13 14 formatted = el.getEffects().stream().map(this::apply).collect(Collec 15 } else { 16 formatted String format("Unknown effect active: %s " effect);
  • 28. #JFall #PatternMatching @hannotify @PeterWessels formatted = String.format( Delay active of %d ms. , de.getTimeInMs() formatted = String.format("Reverb active of type %s and roomSize %d. formatted = String.format("Overdrive active with gain %d.", ov.getGa formatted = String.format("Tremolo active with depth %d and rate %d. formatted = String.format("Tuner active with pitch %d. Muting all si formatted = el.getEffects().stream().map(this::apply).collect(Collec formatted = String.format("Unknown effect active: %s.", effect); 4 } else if (effect instanceof Reverb re) { 5 6 } else if (effect instanceof Overdrive ov) { 7 8 } else if (effect instanceof Tremolo tr) { 9 10 } else if (effect instanceof Tuner tu) { 11 12 } else if (effect instanceof EffectLoop el) { 13 14 } else { 15 16 } 17 return formatted; 18 } 19
  • 29. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression 1 String apply(Effect effect) { 2 return switch(effect) { 3 default -> String.format("Unknown effect active: %s.", eff 4 }; 5 }
  • 30. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression case Delay de -> String.format("Delay active of %d ms.", de.getT case Reverb re -> String.format("Reverb active of type %s and roo String apply(Effect effect) { 1 return switch(effect) { 2 3 4 5 default -> String.format("Unknown effect active: %s.", eff 6 }; 7 }
  • 31. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression 1 String apply(Effect effect) { 2 return switch(effect) { 3 case Delay de -> String.format("Delay active of %d ms.", de.get 4 case Reverb re -> String.format("Reverb active of type %s and ro 5 case Overdrive ov -> String.format("Overdrive active with gain %d." 6 case Tremolo tr -> String.format("Tremolo active with depth %d an 7 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 8 default -> String.format("Unknown effect active: %s.", ef 9 }; 10 }
  • 32. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression case EffectLoop el -> el.getEffects().stream().map(this::apply).coll String apply(Effect effect) { 1 return switch(effect) { 2 case Delay de -> String.format("Delay active of %d ms.", de.get 3 case Reverb re -> String.format("Reverb active of type %s and ro 4 case Overdrive ov -> String.format("Overdrive active with gain %d." 5 case Tremolo tr -> String.format("Tremolo active with depth %d an 6 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 7 8 9 default -> String.format("Unknown effect active: %s.", ef 10 }; 11 }
  • 33. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression String apply(Effect effect) { return switch(effect) { case Delay de -> String.format("Delay active of %d ms.", de.get case Reverb re -> String.format("Reverb active of type %s and ro case Overdrive ov -> String.format("Overdrive active with gain %d." case Tremolo tr -> String.format("Tremolo active with depth %d an case Tuner tu -> String.format("Tuner active with pitch %d. Mut case EffectLoop el -> el.getEffects().stream().map(this::apply).coll default -> String.format("Unknown effect active: %s.", ef }; } 1 2 3 4 5 6 7 8 9 10 11
  • 34. #JFall #PatternMatching @hannotify @PeterWessels https://pxhere.com/en/photo/544037
  • 35. #JFall #PatternMatching @hannotify @PeterWessels Sensible operations to the effect Sensible operations to the effect loop loop apply() setVolume(int volume) contains(Effect... effect)
  • 36. #JFall #PatternMatching @hannotify @PeterWessels Nonsensical operations to the Nonsensical operations to the effect loop effect loop isTunerActive() isDelayTimeEqualToReverbRoomSize() isToneSuitableToPlayPrideInTheNameOfLove()
  • 37. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression String apply(Effect effect) { return switch(effect) { case Delay de -> String.format("Delay active of %d ms.", de.get case Reverb re -> String.format("Reverb active of type %s and ro case Overdrive ov -> String.format("Overdrive active with gain %d." case Tremolo tr -> String.format("Tremolo active with depth %d an case Tuner tu -> String.format("Tuner active with pitch %d. Mut case EffectLoop el -> el.getEffects().stream().map(this::apply).coll default -> String.format("Unknown effect active: %s.", ef }; } 1 2 3 4 5 6 7 8 9 10 11
  • 38. #JFall #PatternMatching @hannotify @PeterWessels Switch expression Switch expression static String apply(Effect effect) { return switch(effect) { case Delay de -> String.format("Delay active of %d ms.", de.get case Reverb re -> String.format("Reverb active of type %s and ro case Overdrive ov -> String.format("Overdrive active with gain %d." case Tremolo tr -> String.format("Tremolo active with depth %d an case Tuner tu -> String.format("Tuner active with pitch %d. Mut case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co default -> String.format("Unknown effect active: %s.", ef }; } 1 2 3 4 5 6 7 8 9 10 11
  • 39. #JFall #PatternMatching @hannotify @PeterWessels Benefits of pattern Benefits of pattern matching matching No need for the Visitor pattern or a common supertype A single expression instead of many assignments Less error-prone (in adding cases) More concise Safer - the compiler can check for missing cases
  • 40. #JFall #PatternMatching @hannotify @PeterWessels But what if But what if effect effect is is null null? ? return switch(effect) { // throws NullPointerException! static String apply(Effect effect) { 1 2 case Delay de -> String.format("Delay active of %d ms.", de.get 3 case Reverb re -> String.format("Reverb active of type %s and ro 4 case Overdrive ov -> String.format("Overdrive active with gain %d." 5 case Tremolo tr -> String.format("Tremolo active with depth %d an 6 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 7 case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co 8 default -> String.format("Unknown effect active: %s.", ef 9 }; 10 } 11
  • 41. #JFall #PatternMatching @hannotify @PeterWessels Combining case labels Combining case labels case null, default -> String.format("Unknown or malfunctioning effec static String apply(Effect effect) { 1 return switch(effect) { 2 case Delay de -> String.format("Delay active of %d ms.", de.get 3 case Reverb re -> String.format("Reverb active of type %s and ro 4 case Overdrive ov -> String.format("Overdrive active with gain %d." 5 case Tremolo tr -> String.format("Tremolo active with depth %d an 6 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 7 case EffectLoop el -> el.getEffects().stream().map(Effect::apply).co 8 9 }; 10 } 11
  • 42. #JFall #PatternMatching @hannotify @PeterWessels Demo Demo Guarded patterns https://pxhere.com/en/photo/1458897
  • 43. #JFall #PatternMatching @hannotify @PeterWessels Guarded patterns Guarded patterns String apply(Effect effect, Guitar guitar) { return switch(effect) { // (...) case Tremolo tr -> String.format("Tremolo active with depth %d and ra case Tuner tu -> String.format("Tuner active with pitch %d. Muting al case EffectLoop el -> el.getEffects().stream().map(this::apply).colle default -> String.format("Unknown effect active: %s.", effect); }; } 1 2 3 4 5 6 7 8 9
  • 44. #JFall #PatternMatching @hannotify @PeterWessels Guarded patterns Guarded patterns case Tuner tu && !tu.isInTune(guitar) -> String.format("Guitar is in String apply(Effect effect, Guitar guitar) { 1 return switch(effect) { 2 // (...) 3 case Tremolo tr -> String.format("Tremolo active with depth %d and ra 4 5 case EffectLoop el -> el.getEffects().stream().map(this::apply).colle 6 default -> String.format("Unknown effect active: %s.", effect); 7 }; 8 } 9
  • 45. #JFall #PatternMatching @hannotify @PeterWessels Guarded patterns Guarded patterns case Tuner tu when !tu.isInTune(guitar) -> String.format("Guitar is i String apply(Effect effect, Guitar guitar) { 1 return switch(effect) { 2 // (...) 3 case Tremolo tr -> String.format("Tremolo active with depth %d and ra 4 5 case EffectLoop el -> el.getEffects().stream().map(this::apply).colle 6 default -> String.format("Unknown effect active: %s.", effect); 7 }; 8 } 9
  • 46. #JFall #PatternMatching @hannotify @PeterWessels Guarded patterns Guarded patterns switch(effect) { // ... case Tuner tu: if (!tu.isInTune(guitar)) { // tuning is needed } else { // no tuning is needed } break; // ... } 1 2 3 4 5 6 7 8
  • 47. #JFall #PatternMatching @hannotify @PeterWessels It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern pattern example guarded pattern Tuner tu when !tu.isInTune(guitar) type pattern Guitar lesPaul https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 48. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Pattern Matching for Switch Pattern Matching for Switch Java version Feature status JEP 17 Preview 18 Second preview 19 Third preview 20 Fourth preview JEP 406 JEP 420 JEP 427 JEP 433
  • 49. #JFall #PatternMatching @hannotify @PeterWessels Deconstructio Deconstructio Deconstructio Deconstructio Deconstructio n Patterns n Patterns n Patterns n Patterns n Patterns https://pxhere.com/en/photo/752901
  • 50. #JFall #PatternMatching @hannotify @PeterWessels Disclaimer Disclaimer Disclaimer Disclaimer Disclaimer We can't tell you when the following features are coming to Java. Also: syntax and implementation specifics may still change. https://pxhere.com/en/photo/1359311
  • 51. #JFall #PatternMatching @hannotify @PeterWessels Deconstruction patterns Deconstruction patterns String apply(Effect effect) { return switch(effect) { case Delay de -> String.format("Delay active of %d ms.", de.get case Reverb re -> String.format("Reverb active of type %s and ro case Overdrive ov -> String.format("Overdrive active with gain %d." case Tremolo tr -> String.format("Tremolo active with depth %d an case Tuner tu -> String.format("Tuner active with pitch %d. Mut case EffectLoop el -> el.getEffects().stream().map(this::apply).coll default -> String.format("Unknown effect active: %s.", ef }; } 1 2 3 4 5 6 7 8 9 10 11
  • 52. #JFall #PatternMatching @hannotify @PeterWessels Deconstruction patterns Deconstruction patterns case Overdrive(int gain) -> String.format("Overdrive active with gai String apply(Effect effect) { 1 return switch(effect) { 2 case Delay de -> String.format("Delay active of %d ms.", de.get 3 case Reverb re -> String.format("Reverb active of type %s and ro 4 5 case Tremolo tr -> String.format("Tremolo active with depth %d an 6 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 7 case EffectLoop el -> el.getEffects().stream().map(this::apply).coll 8 default -> String.format("Unknown effect active: %s.", ef 9 }; 10 } 11
  • 53. #JFall #PatternMatching @hannotify @PeterWessels Pattern definition Pattern definition public class Overdrive implements Effect { private final int gain; public Overdrive(int gain) { this.gain = gain; } } 1 2 3 4 5 6 7
  • 54. #JFall #PatternMatching @hannotify @PeterWessels Pattern definition Pattern definition 1 public class Overdrive implements Effect { 2 private final int gain; 3 4 public Overdrive(int gain) { 5 this.gain = gain; 6 } 7 public pattern Overdrive(int gain) { 8 gain = this.gain; 9 } 10 11 }
  • 55. #JFall #PatternMatching @hannotify @PeterWessels Deconstruction patterns Deconstruction patterns case Overdrive(int gain) -> String.format("Overdrive active with gai String apply(Effect effect) { 1 return switch(effect) { 2 case Delay de -> String.format("Delay active of %d ms.", de.get 3 case Reverb re -> String.format("Reverb active of type %s and ro 4 5 case Tremolo tr -> String.format("Tremolo active with depth %d an 6 case Tuner tu -> String.format("Tuner active with pitch %d. Mut 7 case EffectLoop el -> el.getEffects().stream().map(this::apply).coll 8 default -> String.format("Unknown effect active: %s.", ef 9 }; 10 } 11
  • 56. #JFall #PatternMatching @hannotify @PeterWessels Demo Demo Deconstruction with Records https://pxhere.com/en/photo/1458897
  • 57. #JFall #PatternMatching @hannotify @PeterWessels Deconstruction patterns Deconstruction patterns String apply(Effect effect) { return switch(effect) { case Delay(int timeInMs) -> String.format("Delay active of %d ms.", case Reverb(String name, int roomSize) -> String.format("Reverb acti case Overdrive(int gain) -> String.format("Overdrive active with gai case Tremolo(int depth, int rate) -> String.format("Tremolo active w case Tuner(int pitchInHz) -> String.format("Tuner active with pitch case EffectLoop(Set<Effect> effects) -> effects.stream().map(this::a default -> String.format("Unknown effect active: %s.", effect); }; } 1 2 3 4 5 6 7 8 9 10 11
  • 58. #JFall #PatternMatching @hannotify @PeterWessels Can we combine patterns? Can we combine patterns? static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { } 1 2 3
  • 59. #JFall #PatternMatching @hannotify @PeterWessels Pattern composition Pattern composition static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { if (effectLoop instanceof EffectLoop(Delay(int timeInMs), Reverb(String n return timeInMs == roomSize; 1 2 3 } 4 return false; 5 6 }
  • 60. #JFall #PatternMatching @hannotify @PeterWessels Pattern composition Pattern composition 1 static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { 2 return effectLoop.getEffects().stream() 3 .filter(e -> e instanceof Delay || e instanceof Reverb) 4 .map(dr -> { 5 if (dr instanceof Delay d) { 6 return d.getTimeInMs(); } else { 7 Reverb r = (Reverb) dr; 8 return r.getRoomSize(); 9 } 10 11 }).distinct().count() == 1; 12 13 }
  • 61. #JFall #PatternMatching @hannotify @PeterWessels Var patterns Var patterns // Pre-Java 10 Guitar telecaster = new Guitar("Fender Telecaster Baritone Blacktop", GuitarT // Java 10 var telecaster = new Guitar("Fender Telecaster Baritone Blacktop", GuitarType 1 2 3 4 5 https://openjdk.java.net/jeps/286 https://openjdk.java.net/jeps/286
  • 62. #JFall #PatternMatching @hannotify @PeterWessels Var patterns Var patterns static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { if (effectLoop instanceof EffectLoop(Delay(int timeInMs), Reverb(String n return timeInMs == roomSize; } return false; } 1 2 3 4 5 6
  • 63. #JFall #PatternMatching @hannotify @PeterWessels Var patterns Var patterns if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(var name static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { 1 2 return timeInMs == roomSize; 3 } 4 return false; 5 } 6
  • 64. #JFall #PatternMatching @hannotify @PeterWessels Benefits of Benefits of deconstruction patterns deconstruction patterns Better encapsulation a case branch only receives data that it actually references. More elegant logic by using pattern composition
  • 65. #JFall #PatternMatching @hannotify @PeterWessels It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern pattern example deconstruction pattern Delay(int timeInMs) type pattern Guitar lesPaul guarded pattern Tuner tu when !tu.isInTune(guitar) https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 66. #JFall #PatternMatching @hannotify @PeterWessels It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern pattern example var pattern var timeInMs type pattern Guitar lesPaul guarded pattern Tuner tu when !tu.isInTune(guitar) deconstruction pattern Delay(int timeInMs) https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 67. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Deconstruction patterns Deconstruction patterns Java version Feature status JEP 19 Preview (deconstruction with record patterns only) n/a Second preview (deconstruction with record patterns only) JEP 405 JEP 432
  • 68. #JFall #PatternMatching @hannotify @PeterWessels Pattern Matching Plays Pattern Matching Plays Nice With Nice With Sealed Types Sealed Types Sealed Types Sealed Types Sealed Types and Records and Records and Records and Records and Records https://pxhere.com/en/photo/752901
  • 69. #JFall #PatternMatching @hannotify @PeterWessels Demo Demo Make Effect a sealed type https://pxhere.com/en/photo/1458897
  • 70. #JFall #PatternMatching @hannotify @PeterWessels Sealed types yield completeness Sealed types yield completeness static String apply(Effect effect) { return switch(effect) { case Delay(int timeInMs) -> String.format("Delay active of %d ms.", case Reverb(String name, int roomSize) -> String.format("Reverb acti case Overdrive(int gain) -> String.format("Overdrive active with gai case Tremolo(int depth, int rate) -> String.format("Tremolo active w case Tuner(int pitchInHz) -> String.format("Tuner active with pitch case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effec case EffectLoop(var effects) -> effects.stream().map(Effect::apply). default -> String.format("Unknown effect active: %s.", effect); }; } 1 2 3 4 5 6 7 8 9 10 11 12
  • 71. #JFall #PatternMatching @hannotify @PeterWessels Sealed types yield completeness Sealed types yield completeness static String apply(Effect effect) { return switch(effect) { case Delay(int timeInMs) -> String.format("Delay active of %d ms.", case Reverb(String name, int roomSize) -> String.format("Reverb acti case Overdrive(int gain) -> String.format("Overdrive active with gai case Tremolo(int depth, int rate) -> String.format("Tremolo active w case Tuner(int pitchInHz) -> String.format("Tuner active with pitch case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effec case EffectLoop(var effects) -> effects.stream().map(Effect::apply). 1 2 3 4 5 6 7 8 9 10 }; 11 } https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java
  • 72. #JFall #PatternMatching @hannotify @PeterWessels Records Records Input: Input: Commit to the class being a transparent carrier for its data. Output: Output:
  • 73. #JFall #PatternMatching @hannotify @PeterWessels Patterns in enhanced for Patterns in enhanced for record Delay(int timeInMs) implements Effect {} 1 static void printDelays(List<Delay> delays) { for (Delay delay : delays) { System.out.println("Delay found with timeInMs=" + delay.timeInMs()); } } 1 2 3 4 5
  • 74. #JFall #PatternMatching @hannotify @PeterWessels Patterns in enhanced for Patterns in enhanced for record Delay(int timeInMs) implements Effect {} 1 static void printDelays(List<Delay> delays) { for (Delay(int timeInMs) : delays) { System.out.println("Delay found with timeInMs=" + timeInMs); } } 1 2 3 4 5
  • 75. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Sealed Types Sealed Types Java version Feature status JEP 15 Preview 16 Second preview 17 Final JEP 360 JEP 397 JEP 409
  • 76. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Completeness Completeness Java version Feature status JEP 17 Preview 18 Second preview 19 Third preview JEP 406 JEP 420 JEP 427
  • 77. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Record Patterns Record Patterns Java version Feature status JEP 19 Preview n/a Second preview JEP 405 JEP 432
  • 78. #JFall #PatternMatching @hannotify @PeterWessels A Better A Better A Better A Better A Better Serialization? Serialization? Serialization? Serialization? Serialization? https://pxhere.com/en/photo/752901
  • 79. #JFall #PatternMatching @hannotify @PeterWessels Here be dragons! Here be dragons! Here be dragons! Here be dragons! Here be dragons! We can't be sure at all that the following features will appear in Java as depicted. They can change a lot in the meantime. https://www.pexels.com/photo/dragon-festival-during-nighttime-6068535/
  • 80. #JFall #PatternMatching @hannotify @PeterWessels Opposites Opposites Deconstruction pattern Deconstruction pattern transforms an object into a set of typed fields Constructor Constructor transforms a set of typed fields into an object
  • 81. #JFall #PatternMatching @hannotify @PeterWessels Serialization Serialization very important feature but many people hate its current implementation Drawbacks Drawbacks it undermines the accessibility model serialization logic is not 'readable code' it bypasses constructors and data validation
  • 82. #JFall #PatternMatching @hannotify @PeterWessels Serialization Serialization public class EffectLoop implements Effect { private String name; private Set<Effect> effects; public EffectLoop(String name) { this.name = name; this.effects = new HashSet<>(); } } 1 2 3 4 5 6 7 8 9
  • 83. #JFall #PatternMatching @hannotify @PeterWessels Serialization Serialization 1 public class EffectLoop implements Effect { 2 private String name; 3 private Set<Effect> effects; 4 5 public EffectLoop(String name) { 6 this.name = name; 7 this.effects = new HashSet<>(); 8 } 9 public pattern EffectLoop(String name, Effect[] effects) { 10 name = this.name; 11 effects = this.effects.toArray(); 12 } 13 14 }
  • 84. #JFall #PatternMatching @hannotify @PeterWessels Serialization Serialization public EffectLoop(String name, Effect[] effects) { this(name); for (Effect effect : effects) { this.effects.add(effect); } this.effects = new HashSet<>(); 7 } 8 9 10 11 12 13 14 15 } 16 17 public pattern EffectLoop(String name, Effect[] effects) { 18 name = this.name; 19 effects = this.effects.toArray(); } 20 21 }
  • 85. #JFall #PatternMatching @hannotify @PeterWessels Serialization Serialization public class EffectLoop implements Effect { private String name; private Set<Effect> effects; public EffectLoop(String name) { this.name = name; this.effects = new HashSet<>(); } @Deserializer 1 2 3 4 5 6 7 8 9 10 11 public EffectLoop(String name, Effect[] effects) { 12 this(name); 13 for (Effect effect : effects) { 14 this.effects.add(effect); 15 } 16 }
  • 86. #JFall #PatternMatching @hannotify @PeterWessels Some challenges remain Some challenges remain Q: Q: How to support multiple versions of one class? How to support multiple versions of one class? A: @Serializer and @Deserializer annotations could get a property version in the future.
  • 87. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Java version Feature status JEP n/a Exploratory document Towards Better Serialization https://cr.openjdk.java.net/~briangoetz/amber/serialization.html
  • 88. #JFall #PatternMatching @hannotify @PeterWessels Future Future Future Future Future Expansions Expansions Expansions Expansions Expansions https://pxhere.com/en/photo/752901
  • 89. #JFall #PatternMatching @hannotify @PeterWessels Unnamed patterns Unnamed patterns if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(var name static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { 1 2 return timeInMs == roomSize; 3 } 4 return false; 5 } 6
  • 90. #JFall #PatternMatching @hannotify @PeterWessels Unnamed patterns Unnamed patterns static boolean isDelayTimeEqualToReverbRoomSize(EffectLoop effectLoop) { if (effectLoop instanceof EffectLoop(Delay(var timeInMs), Reverb(_, var r return timeInMs == roomSize; } return false; } 1 2 3 4 5 6
  • 91. #JFall #PatternMatching @hannotify @PeterWessels Optimization Optimization static String apply(Effect effect) { return switch(effect) { case Delay(int timeInMs) -> String.format("Delay active of %d ms.", case Reverb(String name, int roomSize) -> String.format("Reverb acti case Overdrive(int gain) -> String.format("Overdrive active with gai case Tremolo(int depth, int rate) -> String.format("Tremolo active w case Tuner(int pitchInHz) -> String.format("Tuner active with pitch case EffectLoop(var effects) -> effects.stream().map(Effect::apply). default -> String.format("Unknown effect active: %s.", effect); }; } 1 2 3 4 5 6 7 8 9 10 11
  • 92. #JFall #PatternMatching @hannotify @PeterWessels Optimization Optimization 1 static String apply(Effect effect) { 2 return switch(effect) { 3 // ... 4 case EffectLoop(var effects) -> effects.stream().map(Effect::apply).c 5 default -> String.format("Unknown effect active: %s.", effect); 6 }; 7 }
  • 93. #JFall #PatternMatching @hannotify @PeterWessels Optimization Optimization case EffectLoop(Tuner(int pitchInHz), _) -> String.format("The Effect static String apply(Effect effect) { 1 return switch(effect) { 2 // ... 3 4 5 case EffectLoop(var effects) -> effects.stream().map(Effect::apply).c 6 default -> String.format("Unknown effect active: %s.", effect); 7 }; 8 }
  • 94. #JFall #PatternMatching @hannotify @PeterWessels It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern It's a kind of Pattern pattern example unnamed pattern _ type pattern Guitar lesPaul guarded pattern Tuner tu when !tu.isInTune(guitar) deconstruction pattern Delay(int timeInMs) var pattern var timeInMs https://thumbs.gfycat.com/DefiantElasticGadwall.webp
  • 95. #JFall #PatternMatching @hannotify @PeterWessels Feature Status Feature Status Unnamed Patterns Unnamed Patterns Java version Feature status JEP n/a JEP draft 8294349 https://openjdk.org/jeps/8294349 https://openjdk.org/jeps/8294349
  • 96. #JFall #PatternMatching @hannotify @PeterWessels Here be dragons! Here be dragons! Here be dragons! Here be dragons! Here be dragons! We can't be sure at all that the following features will appear in Java as depicted. They can change a lot in the meantime. https://www.pexels.com/photo/dragon-festival-during-nighttime-6068535/
  • 97. #JFall #PatternMatching @hannotify @PeterWessels Pattern bind statements Pattern bind statements var reverb = new Reverb("ChamberReverb", 2); __let Reverb(String name, int roomSize) = reverb; // do something with name & roomSize 1 2 3 4 5 https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java
  • 98. #JFall #PatternMatching @hannotify @PeterWessels Pattern bind statements Pattern bind statements else throw new IllegalArgumentException("not a Reverb!"); var reverb = new Reverb("ChamberReverb", 2); 1 2 __let Reverb(String name, int roomSize) = reverb; 3 4 5 6 // do something with name & roomSize
  • 99. #JFall #PatternMatching @hannotify @PeterWessels Array patterns Array patterns record EffectLoop(String name, int volume, Effect... effects) { } 1 static String apply(EffectLoop effectLoop) {} return switch(effectLoop) { case EffectLoop(var name, var volume) -> "Effect loop contains no eff } } 1 2 3 4 5
  • 100. #JFall #PatternMatching @hannotify @PeterWessels Array patterns Array patterns record EffectLoop(String name, int volume, Effect... effects) { } 1 static String apply(EffectLoop effectLoop) {} return switch(effectLoop) { case EffectLoop(var name, var volume) -> "Effect loop contains no eff case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on 1 2 3 4 5 } 6 }
  • 101. #JFall #PatternMatching @hannotify @PeterWessels Array patterns Array patterns record EffectLoop(String name, int volume, Effect... effects) { } 1 static String apply(EffectLoop effectLoop) {} return switch(effectLoop) { case EffectLoop(var name, var volume) -> "Effect loop contains no eff case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on case EffectLoop(_, _, var effect, ...) -> "Effect loop contains more 1 2 3 4 5 6 } 7 }
  • 102. #JFall #PatternMatching @hannotify @PeterWessels Array patterns Array patterns record EffectLoop(String name, int volume, Effect... effects) { } 1 static String apply(EffectLoop effectLoop) {} return switch(effectLoop) { case EffectLoop(var name, var volume) -> "Effect loop contains no eff case EffectLoop(_, _, var effect) -> "Effect loop contains exactly on case EffectLoop(_, _, var effect, ...) -> "Effect loop contains more case EffectLoop(_, _, var effect1, var effect2) -> "Effect loop conta case EffectLoop(_, _, var effect1, var effect2, ...) -> "Effect loop 1 2 3 4 5 6 7 8 } 9 }
  • 103. #JFall #PatternMatching @hannotify @PeterWessels Other ideas Other ideas Deconstruction patterns for arbitrary classes Enhanced array patterns String[] { [8] -> var eighthElement, [9] -> var ninthElement} AND patterns Patterns in catch clauses Collection patterns https://mail.openjdk.java.net/pipermail/amber-spec-experts/2021-January/002758.html
  • 104. #JFall #PatternMatching @hannotify @PeterWessels Pattern Pattern Pattern Pattern Pattern Contexts Contexts Contexts Contexts Contexts https://pxhere.com/en/photo/752901
  • 105. #JFall #PatternMatching @hannotify @PeterWessels Pattern Contexts Pattern Contexts Pattern context Example Purpose instanceof predicate product instanceof Guitar guitar Test if target matches the indicated pattern. switch statement or expression switch (effect) { case Delay d -> } Test if target matches one (or more) of the indicated patterns. enhanced for for (Delay(int timeInMs) : delays) { } Perform a iteration for each target that matches the indicated pattern. bind statement __let Reverb(var name, var roomSize) = Destructure a target using a pattern.
  • 106. #JFall #PatternMatching @hannotify @PeterWessels Wrap-up Wrap-up Wrap-up Wrap-up Wrap-up https://pxhere.com/en/photo/752901
  • 107. #JFall #PatternMatching @hannotify @PeterWessels Pattern matching... Pattern matching... is a rich feature arc that will play out over several versions. allows us to use type patterns in instanceof. improves switch expressions. makes destructuring objects as easy as (and more similar to) constructing them. holds the potential to simplify and streamline much of the code we write today.
  • 108. #JFall #PatternMatching @hannotify @PeterWessels Major Feature Major Feature Major Feature Major Feature Major Feature
  • 109. #JFall #PatternMatching @hannotify @PeterWessels Thank you! 😀 Thank you! 😀 github.com/hannotify/pattern- matching-music-store hanno.codes peterwessels.nl @hannotify @PeterWessels