table.insert should return the position at which the item was inserted #927
strawbberrys
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
irrelevant question to what you're asking for but wouldn't that code not work in some cases? i.e. say you made 3 connections, and then disconnected the 2nd one, when you go to disconnect the 3rd one the code would error because the 3rd connections position variable is 3, but its actual position in self._callbacks is now 2. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I believe it would be useful for table.insert to return the position at which the item was inserted. Currently, the position an item was inserted at with table.insert is unknown (unless using the optional position argument) which can limit its usage and cause un-needed extra code to be created. Returning the position that the item was inserted at could be very useful for many reasons.
For example, inserting an item into an array using table.insert inside of a function, then returning another function which removes that item from the array. Currently, you would have to insert the item into the array, get the length of the array via the length operator (
#
) and store it into a variable, then finally remove the value with that number. If this change was made, you could simply use the number returned by table.insert and avoid making any extra calls.How it is now:
How it would be:
I don't believe this change would have any major impact on existing code. I don't believe it has any downsides either.
Beta Was this translation helpful? Give feedback.
All reactions