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

Delphi XE6 Firemonkey Generics TuplesMalcolm 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:

Head over and download the Generics.Tuples unit from Github and start using it in your projects.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

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

  1. Joseph G. Mitzen says:

    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. 🙁

    • Admin says:

      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.

  2. Pingback: Four Ways To Parse JSON Into Objects With Delphi XE6 Firemonkey On Android And IOS | Delphi XE5 XE6 Firemonkey, Delphi Android, Delphi IOS

  3. Pingback: Ten Tips For Migrating From C# And .NET To Multi Platform Object Pascal And Delphi Firemonkey | Delphi XE5 XE6 XE7 XE8 Firemonkey, Delphi Android, Delphi IOS

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
LiveBindings Convert Integer Field To Boolean With Delphi XE5 Firemonkey On Android And IOS

Malcolm Groves has a blog post up where he demonstrates how to convert the display of a field from an...