Wednesday, July 1, 2009

One thing I've wished for from time to time when using Java is tuples and maybe some syntactical sugar on top of it for returning multiple values. This happens when I have a method that needs to return one more thing.

One solution I've used in the past was to pass in an array, and put the value to be returned in the array. It's really ugly, and it's not my preferred way of doing things.

What I do now is declare a new class to hold the multiple values.

It would be nicer to be able to be able to declare something like this

Tuple<Integer,Integer,Integer> get3Numbers();

and then call it with

a, b, c = get3Numbers();

or

(a, b, c) = get3Numbers();

where a, b, and c are ints in this example.

No comments:

Post a Comment