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

Other comments have linked newer language features that make it easy. But for years, the Java Way of handling discriminated unions was to use the visitor pattern [1]. It's very verbose, and is an insane amount of typing unless your IDE is doing the typing for you, but it has the compile time guarantees that forces each caller to handle every type without instanceof/Object.

[1] https://dzone.com/articles/design-patterns-visitor




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

Search: