News for hackers. Stuff that matters.
Re:Selecting a programming language can be a form of prematu... by taeric on Monday November 15, 2021 @11:53AM (#29229489)
First, I want to make it clear that if you have a programming style that works for you, please don't take this as an attack against it. Keep using what works.

If you find yourself commonly having to cast to/from Object in Java, that is almost certainly something of your own doing.

Re:Selecting a programming language can be a form of prematu... by ubercore on Monday November 15, 2021 @12:04PM (#29229606)
What's the "Java way" of accomplishing something like you'd represent with Union or Intersection types?
Re:Selecting a programming language can be a form of prematu... by avita1 on Monday November 15, 2021 @03:41PM (#29232422)
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

Re:Selecting a programming language can be a form of prematu... by taeric on Tuesday November 16, 2021 @10:09AM (#29241347)
Apologies for forgetting I had posted this... :(

My gut would be a visitor for the fully general case. Though, I would also accept that you are using the wrong language for the abstractions your are writing? My guess is it will depend on why you are needing/wanting one of those types.

For a lot of plumbing code, these aren't as necessary as stipulated. Yes, there could be some duplication of code. But, especially in a microservice landscape, most of that duplication will be each service doing their version of whatever you are doing.

Re:Selecting a programming language can be a form of prematu... by programmer_dude on Monday November 15, 2021 @12:28PM (#29229930)
There is none, without up or down casting. This is just some "guideline" that gets paraded from time to time (in OOP circles). On second thought, I am sure there's a "design pattern" for sum types out there. Why take the easy way out, am I right?
Re:Selecting a programming language can be a form of prematu... by dljsjr on Monday November 15, 2021 @12:25PM (#29229872)
A common interface with all of the required functionality exposed as interface methods. A Java person would argue that if you have a function that takes an object that can be two totally disparate things with no shared functionality then that’s a code smell.
Re:Selecting a programming language can be a form of prematu... by tomtheelder on Monday November 15, 2021 @12:36PM (#29230021)
Probably with a composite class I guess. So a union type of types A and B has members of types A and B, and a non-nullable flag to indicate which one this instance is. Intersection type is just the same thing without the flag.

I don't write Java, though, just guessing.

Re:Selecting a programming language can be a form of prematu... by mumblemumble on Monday November 15, 2021 @01:00PM (#29230324)
It's one option. It has the advantage of structurally limiting the types that can be used in that spot. It has the disadvantage (compared to true union types) of offering no static help beyond that.

If you add a case, for example, you're 100% on your own to make sure that all code that interacts with the type is updated to handle the new case. Slip-ups will produce run-time errors rather than static ones.

It's also rather tricky (and awkward) to set things up such that consumers are forced to check the case value before attempting to coerce it.

All of this can be worked around with custom linter rules, but then that becomes its own maintenance burden.

My preference is to try and invert things such that you can rely on dynamic dispatch and "tell, don't ask." That eliminates the need to coerce things at run-time in the first place.

Re:Selecting a programming language can be a form of prematu... by Someone on Monday November 15, 2021 @12:59PM (#29230297)
You can also implement an union type as a sealed interface/abstract class with two classes that implement/extend it (https://openjdk.java.net/jeps/409, https://www.baeldung.com/java-sealed-classes-interfaces)