diff --git a/slides.md b/slides.md index fb5ff74..20fe91a 100644 --- a/slides.md +++ b/slides.md @@ -145,6 +145,20 @@ --- +# Records: Constructor canĂ³nico + +```java +public record Movie(String title, List cast) { + public Movie { + cast = List.copyOf(cast); + } +} +``` + +* copia defensiva para evitar modificar el contenido del record. + +--- + # Sealed classes and interfaces :closed_lock_with_key: ```java @@ -287,7 +301,7 @@ * Productos y sumas de tipos. `a + b` `a * b` * Cardinalidad. -* Se les llama algebraicos porque tienen propiedades algebraicas. +* Tienen propiedades algebraicas. --- diff --git a/snippets/Movie.java b/snippets/Movie.java index 817989e..caeb257 100644 --- a/snippets/Movie.java +++ b/snippets/Movie.java @@ -1,8 +1,11 @@ -public record Movie(String title, int year, int duration) { +import java.util.List; + +public record Movie(String title, int year, int duration, List cast) { public Movie { if (title == null || title.isEmpty()) { throw new IllegalArgumentException(); } + cast = List.copyOf(cast); } }