News for hackers. Stuff that matters.
Re:Java record pattern matching in JDK 19 by arthurcolle on Saturday May 14, 2022 @05:50PM (#31382936)
> All the other languages I know of, like this proposal for Java, require the variable to be redefined.

can you give a few examples? This makes no sense to me and I've touched quite a few ecosystems in my time. Maybe I am misunderstanding but my current understanding makes this seem not realistic

Re:Java record pattern matching in JDK 19 by gizmo686 on Sunday May 15, 2022 @12:21AM (#31385236)
Consider the following pseudo-Java:

    Object foo = ...
    if (foo instanceof String) {
        System.out.println(foo.toLowerCase())
    }
The above is not valid Java, because foo has type Object, which does not have a toLowerCase() method, so the compiler refuses to compile it. What you need to do is something like:

    Object foo = ...
    if (foo instanceof String) {
        String sfoo = (String) foo
        System.out.println(sfoo.toLowerCase())
    }
Granted, you don't necessarily need to explicitly redefine in, as Java allows anonymous casting:

    Object foo = ...
    if (foo instanceof String) {
        System.out.println(((String) foo).toLowerCase())
    }
Regardless, the point still stands. Java (and most strongly typed languages with an inheritance based type system) requires an explicit cast, even if a straight forward static analysis would show that the object in question is already the desired type.
Re:Java record pattern matching in JDK 19 by GavinBierman on Monday May 16, 2022 @05:20AM (#31394947)
This is all true, but with pattern matching for instanceof you don't need the explicit cast:

    Object foo = ...
    if (foo instanceof String s) {
        System.out.println(s.toLowerCase())
    }
In this case, the scope of the pattern variable `s` is just the then block, so are free to reuse it elsewhere in the block.