diff --git a/build.sh b/build.sh index 2b5e8a2..c7965b0 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/bash -marp --template bespoke slides.md -o slides.html +marp slides.md -o slides.html marp slides.md -o slides.pdf \ No newline at end of file diff --git a/slides.md b/slides.md index 407cd5e..749d00f 100644 --- a/slides.md +++ b/slides.md @@ -36,6 +36,8 @@ * Todavía Java 8? :cry: * Y anteriores a Java 8? :exploding_head: + + --- # Java 8 @@ -52,10 +54,10 @@ | Java20 | 11% | + No existía ni tiktok ni openai + Ese mismo año se lanzó spring-boot v1.0.0 + Rajoy Presidente + JuanCar I abdicó en Felipe VI --> --- @@ -63,9 +65,12 @@ * El largo camino a Java 21. * Tipos de datos algebraicos. -* Ejemplos. * Futuro. + + --- # Java Release Cadence :coffee: @@ -78,10 +83,6 @@ # Switch expressions -* Incluido en Java 14. -* Una nueva vida para `switch`. -* Una gran mejora en general para el lenguage. - ```java var value = switch (input) { case "a" -> 1; @@ -90,27 +91,28 @@ }; ``` +* Incluido en Java 14. +* Una nueva vida para `switch`. +* Una gran mejora en general para el lenguage. + --- # Records -* Incluido en Java 16. -* Muy esperado por la comunidad. -* Inmutables. - ```java public record Movie(String title, int year, int duration) { } ``` +* Incluido en Java 16. +* Muy esperado por la comunidad. +* Inmutables. + --- # Sealed classes and interfaces -* Incluido en Java 17. -* Jerarquías de clases cerradas. - ```java public sealed interface Shape { @@ -121,13 +123,13 @@ } ``` +* Incluido en Java 17. +* Jerarquías de clases cerradas. + --- # Pattern matching for switch -* Incluido en Java 21. -* Exhaustiveness. - ```java var result = switch (obj) { case Integer i -> String.format("int %d", i); @@ -138,13 +140,13 @@ }; ``` +* Incluido en Java 21. +* Exhaustiveness. + --- # Record patterns -* Incluido en Java 21. -* Deconstructores, nos permite acceder a los componentes de los objectos. - ```java var area = switch (this) { case Square(var side) -> side * side; @@ -153,6 +155,9 @@ }; ``` +* Incluido en Java 21. +* Deconstructores, nos permite acceder a los componentes de los objectos. + --- # Tipos de datos algebraicos @@ -166,25 +171,39 @@ # Tipos de datos algebraicos (II) -* Podemos representarlos en Java. +* ¿Cómo podemos representarlos en Java? * Un `record` es un producto de tipos. * Un `sealed interface` es una suma de tipos. +* ¿Pero eso para qué me sirve? + +--- + +# Tipos de datos algebraicos: List ```java -public sealed interface Tree { - record Node(String value, Tree left, Tree right) implements Tree { } - record Leaf(String value) implements Tree {} +sealed interface List { + record NonEmpty(T head, List tail) implements List {} + record Empty() implements List {} } ``` --- -# Tipos de datos algebraicos (III) - -* Podemos representar `Optional` con ADTs +# Tipos de datos algebraicos: Tree ```java -public sealed interface Optional { +sealed interface Tree { + record Node(T value, Tree left, Tree right) implements Tree { } + record Leaf(T value) implements Tree {} +} +``` + +--- + +# Tipos de datos algebraicos: Optional + +```java +sealed interface Optional { record Empty() implements Optional { } record Present(T value) implements Optional {} } @@ -192,12 +211,10 @@ --- -# Tipos de datos algebraicos (IV) - -* Podemos representar `Json` con ADTs +# Tipos de datos algebraicos: Json ```java -public sealed interface Json { +sealed interface Json { enum JsonNull implements Json { NULL } enum JsonBoolean implements Json { TRUE, FALSE } record JsonString(String value) implements Json {} @@ -209,10 +226,20 @@ --- -# Ejemplo +# Tipos de datos algebraicos: Errores -* ¿Pero eso para qué me sirve? -* Manejo de errores. +```java +sealed interface MovieError extends MovieResponse { + record DuplicatedMovie(UUID id) implements MovieError {} + record InvalidDuration(int duration) implements MovieError {} + record InvalidYear(int year) implements MovieError {} + record InvalidStars(int stats) implements MovieError {} + record EmptyTitle() implements MovieError {} + record EmptyDirector() implements MovieError {} + record EmptyCast() implements MovieError {} + record DuplicatedActor(String actor) implements MovieError {} +} +``` --- @@ -224,10 +251,6 @@ # Unnamed variables and patterns -* Incluido en Java 22. -* Mejora para el pattern matching. -* Eliminar verbosidad. - ```java var result = switch (obj) { case Integer _ -> "int"; @@ -238,12 +261,14 @@ }; ``` +* Incluido en Java 22. +* Mejora para el pattern matching. +* Eliminar verbosidad. + --- # Primitive types in patterns -* Preview en Java 23 (sep 2024). - ```java var result = switch (obj) { case int i -> String.format("int %d", i); @@ -254,6 +279,8 @@ }; ``` +* Preview en Java 23 (sep 2024). + --- # Bola de cristal :crystal_ball: @@ -264,9 +291,6 @@ # Derived Record Creation -* Draft. -* AKA withers. - ```java Point newPoint = oldPoint with { x *= 2; @@ -274,15 +298,13 @@ }; ``` +* Draft. +* AKA withers. + --- # Member patterns -* Early work. -* AKA deconstructors. -* Mejora para pattern matching. -* Cualquier clase. - ```java var result = switch (optional) { case Optional.of(var value) -> value.toString(); @@ -290,6 +312,11 @@ }; ``` +* Early work. +* AKA deconstructors. +* Mejora para pattern matching. +* Cualquier clase. + --- # ¿Qué falta todavía? @@ -297,6 +324,9 @@ * Tail recursion. * Soporte de tipos de datos primitivos en genericos. + + --- # ¿Preguntas? @@ -311,7 +341,7 @@ --- -# Links +# JEPs - [Switch expressions](https://openjdk.org/jeps/361) 14 - [Records](https://openjdk.org/jeps/395) 16 @@ -320,4 +350,12 @@ - [Record patterns](https://openjdk.org/jeps/440) 21 - [Unnamed variables and patterns](https://openjdk.org/jeps/456) 22 - [Primitive types in patterns](https://openjdk.org/jeps/455) 23 (1st preview) +- [Derived record creation](https://openjdk.org/jeps/468) Candidate + + + +--- + +# Links + - [Data Oriented Programmig](https://www.infoq.com/articles/data-oriented-programming-java/) Brian Goetz \ No newline at end of file diff --git a/snippets/Expr.java b/snippets/Expr.java index 8ad42bb..9b9067a 100644 --- a/snippets/Expr.java +++ b/snippets/Expr.java @@ -1,6 +1,6 @@ package snippets; -public sealed interface Expr { +sealed interface Expr { record Lit(int value) implements Expr {} record Sum(Expr left, Expr right) implements Expr {} diff --git a/snippets/Json.java b/snippets/Json.java index b51dc1b..dad806d 100644 --- a/snippets/Json.java +++ b/snippets/Json.java @@ -3,7 +3,7 @@ import java.util.List; import java.util.Map; -public sealed interface Json { +sealed interface Json { enum JsonNull implements Json { NULL } enum JsonBoolean implements Json { TRUE, FALSE } record JsonString(String value) implements Json {} diff --git a/snippets/List.java b/snippets/List.java new file mode 100644 index 0000000..227b964 --- /dev/null +++ b/snippets/List.java @@ -0,0 +1,6 @@ +package snippets; + +sealed interface List { + record NonEmpty(T head, List tail) implements List {} + record Empty() implements List {} +} diff --git a/snippets/MovieResponse.java b/snippets/MovieResponse.java index cd97606..f8c0b6c 100644 --- a/snippets/MovieResponse.java +++ b/snippets/MovieResponse.java @@ -2,7 +2,7 @@ import java.util.UUID; -public sealed interface MovieResponse { +sealed interface MovieResponse { record MovieCreated(UUID id) implements MovieResponse {} sealed interface MovieError extends MovieResponse { record DuplicatedMovie(UUID id) implements MovieError {} diff --git a/snippets/Optional.java b/snippets/Optional.java index b419128..5537351 100644 --- a/snippets/Optional.java +++ b/snippets/Optional.java @@ -1,6 +1,38 @@ package snippets; -public sealed interface Optional { +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +sealed interface Optional { record Empty() implements Optional { } record Present(T value) implements Optional {} -} + + default Optional map(Function mapper) { + return switch (this) { + case Present(T value) -> new Present<>(mapper.apply(value)); + case Empty() -> new Empty<>(); + }; + } + + default Optional flatMap(Function> mapper) { + return switch (this) { + case Present(T value) -> mapper.apply(value); + case Empty() -> new Empty<>(); + }; + } + + default R fold(Supplier onEmpty, Function onPresent) { + return switch (this) { + case Present(T value) -> onPresent.apply(value); + case Empty() -> onEmpty.get(); + }; + } + + default Optional filter(Predicate filter) { + return switch (this) { + case Present(T value) when filter.test(value) -> this; + default -> new Empty<>(); + }; + } +} diff --git a/snippets/Shape.java b/snippets/Shape.java index 16e20a1..b00a3b0 100644 --- a/snippets/Shape.java +++ b/snippets/Shape.java @@ -1,6 +1,6 @@ package snippets; -public sealed interface Shape { +sealed interface Shape { default double area() { var area = switch (this) { diff --git a/snippets/Switches.java b/snippets/Switches.java index 0ff0a40..0973c82 100644 --- a/snippets/Switches.java +++ b/snippets/Switches.java @@ -1,6 +1,6 @@ package snippets; -public class Switches { +class Switches { void method1(String input) { var value = switch (input) { @@ -21,5 +21,5 @@ }; System.out.println(result); } - + } diff --git a/snippets/Tree.java b/snippets/Tree.java index 687595d..d5fd876 100644 --- a/snippets/Tree.java +++ b/snippets/Tree.java @@ -1,6 +1,6 @@ package snippets; - -public sealed interface Tree { - record Node(String value, Tree left, Tree right) implements Tree { } - record Leaf(String value) implements Tree {} -} + +sealed interface Tree { + record Node(T value, Tree left, Tree right) implements Tree { } + record Leaf(T value) implements Tree {} +}