Hacker News new | ask | show | jobs
by masklinn 5493 days ago
> But I'm not sure how to implement the generic class Either in Java.

An abstract class with two concrete private implementations and the relevant factory methods (probably on the abstract class).

The part which is going to break you is handling the type-safe unwrapping of error or value. You could do that with blocks/lambdas if Java had blocks/lambdas which worked (Smalltalk-style), but it does not.

Oh, and I think you can't make the abstract class final since you need to extend it, so others are free to extend it as well, and then you're screwed.

I tried implementing a Maybe type in C# once, it did not end well (in the sense that I could not really enforce type-safety, if I remember well), and I'd expect Java would be even worse.