News for hackers. Stuff that matters.
Re:Why don't more languages offer flow typing? by timmg on Monday April 04, 2022 @11:46AM (#30908012)
I was going to point this out, too.

I thought it also supported cases like:

if (foo instanceof Bar) { // foo is typed as Bar in this block }

Is that true, or did I get that wrong?

Re:Why don't more languages offer flow typing? by qw on Monday April 04, 2022 @12:20PM (#30908433)

  if (o instanceof String s) {
    var foo = s.indexOf("bar");
  }
Java 17 introduced a preview of the same feature for switch statements

  return switch (o) {
     case Integer i -> String.format("int %d", i);
     case Long l    -> String.format("long %d", l);
     case Double d  -> String.format("double %f", d);
     case String s  -> String.format("String %s", s);
     default        -> o.toString();
  };
Future versions will have improved support: http://openjdk.java.net/jeps/420)
Re:Why don't more languages offer flow typing? by MH15 on Monday April 04, 2022 @04:42PM (#30911709)
I use the `o instanceof Class c` thing all the time in my hobby compiler on Java 16 right now. The Java 17 version will greatly simplify much of my codebase.