diff --git a/slides.md b/slides.md index e54814f..fb5ff74 100644 --- a/slides.md +++ b/slides.md @@ -190,6 +190,21 @@ --- +# Null patterns + +```java +var result = switch (obj) { + case null -> "null"; + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> obj.toString(); +}; +``` + +--- + # Record patterns ```java @@ -204,8 +219,6 @@ * Deconstructores, nos permite acceder a los componentes de los objectos. * Exhaustiveness. - - --- # Guarded patterns @@ -221,6 +234,20 @@ --- +# Nested patterns + +```java +var result = switch (this) { + case Square(Point(var x, var y), var side) -> ...; + case Rectangle(Point(var x, var y), var weight, var height) -> ...; + case Circle(Point(var x, var y), var radious) -> ...; +}; +``` + +* Podemos anidar patrones + +--- + # Unnamed variables and patterns ```java @@ -636,18 +663,6 @@ - reduce cognitive load. - pattern exhaustiveness. - features: - - enum - - null checks - - partial / nested patterns - - other languages: - - scala - - kotlin - - haskell - - rust - - C# - algebraic data types: - algegraic: because it has properties like an algebra - product type diff --git a/snippets/Switches.java b/snippets/Switches.java index a25ee71..cc1e224 100644 --- a/snippets/Switches.java +++ b/snippets/Switches.java @@ -48,4 +48,14 @@ return 0; } + void method5(Object obj) { + var result = switch (obj) { + case null -> "null"; + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> obj.toString(); + }; + } }