נבנה על ידיBuilt by Ariel Yohimovich
record Book(String title, int year) {}:record: record Book(String title, int year) {}:Book { if (year < 0) throw ...; } בתוך record:compact constructor: Book { if (year < 0) throw ...; } inside a record:sealed interface Shape permits Circle, Rect:sealed: sealed interface Shape permits Circle, Rect:switch (s) { case Circle c -> ...; } בודק את הסוג ונותן משתנה מוכן.pattern switch: switch (s) { case Circle c -> ...; } checks the type and gives a ready variable.case Rect(double w, double h) -> מפרק את ה-record לשדות שלו ישר בתוך ה-case.record pattern: case Rect(double w, double h) -> takes the record apart into its fields right in the case.record Book נותן toString, getters (title(), בלי get), ו-equals/hashCode לפי כל השדות, ולכן ה-Set רואה ספר אחד.record Book gives a toString, getters (title(), no get), and equals/hashCode by every field, so the Set sees one book.record Book נותן toString, getters (title(), בלי get), ו-equals/hashCode לפי כל השדות, ולכן ה-Set רואה ספר אחד. הבנאי המקוצר בודק את השנה.record Book gives a toString, getters (title(), no get), and equals/hashCode by every field, so the Set sees one book. The compact constructor checks the year.Genre נוצר פעם אחת עם הנתונים שלו.Each Genre value is created once with its data.values() מחזירה את כולם לפי הסדר, ו-valueOf הופך טקסט לערך.values() returns them all in order, and valueOf turns text into a value.Genre נוצר פעם אחת עם הנתונים שלו. values() מחזירה את כולם לפי הסדר, ו-valueOf הופך טקסט לערך.Each Genre value is created once with its data. values() returns them all in order, and valueOf turns text into a value.Shape סגור: יש רק Circle ו-Rect.Shape is sealed: there's only Circle and Rect.when מוסיף תנאי (ריבוע).The switch takes each record apart (record pattern), and when adds a condition (a square).default: javac יודע שכיסינו הכול.There's no default: javac knows we covered everything.Shape סגור: יש רק Circle ו-Rect. ה-switch מפרק כל record (פירוק record), ו-when מוסיף תנאי (ריבוע). אין default: javac יודע שכיסינו הכול.Shape is sealed: there's only Circle and Rect. The switch takes each record apart (record pattern), and when adds a condition (a square). There's no default: javac knows we covered everything.Triangle לרשימת ה-permits, ושכחנו לעדכן את ה-switch.We added Triangle to the permits list and forgot to update the switch.Triangle לרשימת ה-permits, ושכחנו לעדכן את ה-switch. javac לא נותן לזה לעבור: זה היתרון של כיסוי מלא.We added Triangle to the permits list and forgot to update the switch. javac won't let it pass: that's the payoff of exhaustiveness.the switch expression does not cover all possible input values:the switch expression does not cover all possible input values:sealed interface Item permits PrintBook, EBook, AudioBook, שלושתם records. describe(Item) עם פירוק records, ו-loanDays(Item) (ספר מודפס 14, ebook 21, ספר שמע 28 אם הוא מעל 600 דקות, אחרת 14). הדפיסו:
Emma: print, 474 pages (14 days) / Dune: ebook, 2.5 MB (21 days) / Ulysses: audio, 12h30m (28 days).sealed interface Item permits PrintBook, EBook, AudioBook, all three records. describe(Item) with record patterns, and loanDays(Item) (print 14, ebook 21, audio 28 if over 600 minutes, otherwise 14). Print:
Emma: print, 474 pages (14 days) / Dune: ebook, 2.5 MB (21 days) / Ulysses: audio, 12h30m (28 days).record Book(String title, int year) {}:record: record Book(String title, int year) {}:Book { if (year < 0) throw ...; } בתוך record:compact constructor: Book { if (year < 0) throw ...; } inside a record:sealed interface Shape permits Circle, Rect:sealed: sealed interface Shape permits Circle, Rect:switch (s) { case Circle c -> ...; } בודק את הסוג ונותן משתנה מוכן.pattern switch: switch (s) { case Circle c -> ...; } checks the type and gives a ready variable.case Rect(double w, double h) -> מפרק את ה-record לשדות שלו ישר בתוך ה-case.record pattern: case Rect(double w, double h) -> takes the record apart into its fields right in the case.record Book נותן toString, getters (title(), בלי get), ו-equals/hashCode לפי כל השדות, ולכן ה-Set רואה ספר אחד.record Book gives a toString, getters (title(), no get), and equals/hashCode by every field, so the Set sees one book.Genre נוצר פעם אחת עם הנתונים שלו.Each Genre value is created once with its data.values() מחזירה את כולם לפי הסדר, ו-valueOf הופך טקסט לערך.values() returns them all in order, and valueOf turns text into a value.Shape סגור: יש רק Circle ו-Rect.Shape is sealed: there's only Circle and Rect.when מוסיף תנאי (ריבוע).The switch takes each record apart (record pattern), and when adds a condition (a square).default: javac יודע שכיסינו הכול.There's no default: javac knows we covered everything.Triangle לרשימת ה-permits, ושכחנו לעדכן את ה-switch.We added Triangle to the permits list and forgot to update the switch.the switch expression does not cover all possible input values:the switch expression does not cover all possible input values:record P(int x, int y) {}. איך קוראים את x?record P(int x, int y) {}. How do you read x?p.getX()p.getX()p.x()p.x()p.xp.xP.xP.xp.x()p.x()sealed ב-switch?What's the benefit of sealed in a switch?record Point(int x, int y) עם מתודה distance() ומתודה withX(int) שמחזירה נקודה חדשה.record Point(int x, int y) with a distance() method and a withX(int) method returning a new point.record Rating(int value) עם בנאי מקוצר שזורק Rating must be 1-5, got 9.record Rating(int value) with a compact constructor throwing Rating must be 1-5, got 9.enum Size { SMALL(10), MEDIUM(14), LARGE(18) } עם price().enum Size { SMALL(10), MEDIUM(14), LARGE(18) } with price().enum Day (ראשון עד שבת).enum Day (Sunday to Saturday).record Book(String title, int year) {}:record: record Book(String title, int year) {}:record Book נותן toString, getters (title(), בלי get), ו-equals/hashCode לפי כל השדות, ולכן ה-Set רואה ספר אחד.record Book gives a toString, getters (title(), no get), and equals/hashCode by every field, so the Set sees one book.Genre נוצר פעם אחת עם הנתונים שלו.Each Genre value is created once with its data.Shape סגור: יש רק Circle ו-Rect.Shape is sealed: there's only Circle and Rect.Triangle לרשימת ה-permits, ושכחנו לעדכן את ה-switch.We added Triangle to the permits list and forgot to update the switch.the switch expression does not cover all possible input values:the switch expression does not cover all possible input values:נבנה על ידיBuilt by Ariel Yohimovich