Several know the concept of a : a custom grouping of values (or references thereto) that can be created at will and returned from a process.

Some people have claimed that doesn't allow such behavior.

It does.

Well: kind-of.

The problem with java up until version 11 is that is didn't have type inference, which makes returning tuples quite elaborate, but not impossible.


See, in all data types that aren't primitives inherit from the Object class. And that means that any process could generate an Object as its return value. It would be a single compound value, the elements of which can be accessed by index.

Up until however, if we were to access those elements, we were responsible for telling the compiler what we expected, and we would have to perform a type cast.

And the compiler couldn't check for type safety.

How different is this with a like , which have tuples as a language construct:

With these languages the compiler does know what data types the tuple elements should be. They do not require us to program an additional type check or type cast.

and are weakly typed, which means we don't have to cast, but we still have to check the data type ourselves.

The common solution in the is not to return at all. Instead, we expect the programmer to create a custom data type which holds and can access a tuple's elements as strongly, statically typed class instance fields.

This is normal, for java.

It's also a lot of work.

It's verbose, repetitive, and each type requires a ton of testing, which is equally verbose and repetitive.

Time for java to embrace tuples!

@aeveltstra Verbose and repetitive, sounds like Java!

At least it's not as bad as writing GObject code in C.

@phoe Awesome! And sad at the same time. I've come across several differing tuple implementations. Including one I made myself due to specific needs.

Instead of everyone being forced to choose or create their own tuple mechanism, the programming language should implement tuple return values as a language feature.

@phoe I'll get there one day, if I feel like spending an inordinate amount of time on this language!

Sign in to participate in the conversation

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!