Add Generic Tuple Support With Generics.Tuples In Firemonkey On Android And IOS


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:
LTuple : ITuple<Integer, String>;
LTuple := TTuple<Integer, String>.Create(10, ‘Hello’);
x := LTuple.Value1 + 10;
Head over and download the Generics.Tuples unit from Github and start using it in your projects.

Comments are closed.