3 ms·
”I thought multiple return was more interesting in the context of multiple possible types: like a type in case of success and one in case of failure” That’s or
by Someone 7y ago
”I thought multiple return was more interesting in the context of multiple possible types: like a type in case of success and one in case of failure”
That’s or (another) one in case of failure, and not multiple return values. It returns a single value.
”I never had the problem with returning several objects at ones, I would simply write a class that embeds them”
One man’s “simply” is another man’s “ugly boilerplate” or “unnecessary production of garbage objects”.
You could similarly ”simpler write a class that embeds either of them”, like this:
class AorB {
private final Boolean itsAnA;
private final A a;
private final B b;
public AorB(A aa) {
a = aa;
b = null;
itsAnA = true;
}
public AorB(B bee) {
a = null;
b = bee;
itsAnA = false;
}
...
}
Ugly boilerplate? Yes. Not optimally efficient? Yes. Not as nice as the real thing? Yes. Workable? Yes. Good idea? Debatable. Java culture would use an exception if B is an error type.
- cies 7y agoI wonder what the "match" statement would look like on your example. This is exactly as you say though: ugly, inefficient, not-as-nice, yet works. And clearly debatable, especially as it goes against Java culture. The pattern could be used commonly, and clean-up heaps of code, and remove exception's hard-to-wrap-yr-mind-around flow jumps/etc, that I do hope for both tagged unions and match switches on them to be introduced. Some are already demo features IIRC.