Hacker News new | past | comments | ask | show | jobs | submit login

Java does have exhaustiveness checking on switch statements for sealed types, but not enums (for backward compatibility), which is probably what you're using.

Try this:

    sealed interface Shape permits Circle, Rectangle { }
    record Circle(double radius) implements Shape { }
    record Rectangle(double length, double width) implements Shape { }

    void test() {
        Shape shape = new Circle(5);
        switch (shape) {
            case Circle _ -> System.out.println("Circle");
            case Rectangle _ -> System.out.println("Rectangle"); // Comment out this line to get an error.
        }
    }



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: