Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yeah, Java's generics still kind of suck. There's a rumor after project Valhalla, Java language maintainers might add reified generics in the future. However, I think the current state of Java ADT & generic isn't that bad for most purposes.

Though due to its nullable-by-default type system and backward compatibility, there's a decent amount of footguns if you're trying to mix Java's FP & ADT with code that utilizes nulls.

About your code example, you could just do something like this to avoid explicit casting

  sealed interface Result<T,E> {
      record Ok<T,E>(T value) implements Result<T,E> {}
      record Error<T,E>(E error) implements Result<T,E> {}
      public static <T,E> Object eval(Result<T,E> res) {
          if (res instanceof Error<T,E>(E e)) // Rust's if let
              System.out.println(e);
          return switch (res) {
              case Ok(T v) -> v;
              case Error(E e) -> e;
          };
      }
  }
The new "pattern matching" feature of `instanceof` is certainly handy to avoid stupid ClassCastException.


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

Search: