Malcolm Groves from Embarcadero has a new unit posted on Github called Generics.Tuples and it implements generic tuple support in Delphi XE5, Delphi XE6 and AppMethod. It should also work cross platform on Android, IOS, Windows, and OSX. Tuples are a single type that can hold multiple other types. Malcolm used the generics capability of Object Pascal to implement two and three parameter tuples. A sample usage for a tuple that he gives is being able to return two values from a function instead of just one. It currently supports tuples containing two and three items. I checked out the Microsoft docs for the C# tuples object and the Microsoft one supports up to 8. You can easily add more than the 3 it currently has to this Object Pascal implementation if you need more. Here is his sample code for using a tuple to store two different values:
var
LTuple : ITuple<Integer, String>;
begin
LTuple := TTuple<Integer, String>.Create(10, 'Hello');
x := LTuple.Value1 + 10;
ShowMessage(LTuple.Value2);
Head over and download the Generics.Tuples unit from Github and start using it in your projects.
While it’s nice work (and raises questions about why EMBT itself isn’t showing this kind of initiative with the language, I’m not really sure how useful it is without more native support and some other features. Is it garbage-collected? By needing to reference “Value1” in your code example, does that mean it can’t be accessed by index like tuples in other languages? What about iterating through the values? It looks like the only way to assign multiple values at once is through the create method… unless there’s another method to assign multiple values again?
Tuples often go hand-in-hand with packing/unpacking language support. For instance, if a function returned a tuple, one could write in Oxygene (and Swift and Python, etc.) something like:
(firstName, Lastname) := SomeFunc(whatever);
and the returned tuple would be unpacked into the two variables firstName and lastName.
>A sample usage for a tuple that he gives is being able to return two values from a function instead of just one.
We can do that now with records. The real use comes in the automatic unpacking and the arbitrary types. Needing to declare a generic-infused tuple beforehand, without even the benefit of type inference, and no unpacking doesn’t really gain anything.
The limitations of generics are also shown in that one is going to need to manually create versions of this tuple that work for each specific number of parameters.
This seems more like a simple shorthand for creating records than any resemblance to the tuple type as implemented in other languages. Again, nice effort though. But if the author is with Embarcadero why isn’t their code aimed at Delphi itself rather than being a personal project on Github? That sends the depressing message that EMBT itself isn’t interested in tuples. 🙁
I agree it basically would let you skip defining a record type at the top of the file (for up to 8 properties if 8 were defined). In your example you could just do SomeFunc(firstName, lastName, whatever); in Delphi. Less intuitive than assigning them with a := though.