table.clear

Note: this RFC was adapted from an internal proposal that predates RFC process and as such doesn’t follow the template precisely

Status: Implemented

Summary

Add table.clear function that removes all elements from the table but keeps internal capacity allocated.

Design

table.clear adds a fast way to clear a Lua table. This is effectively a sister function to table.create(), only for reclaiming an existing table’s memory rather than pre-allocating a new one. Use cases:

These use cases can technically be accomplished via table.move moving from an empty table to the table which is to be edited, but I feel that they are frequent enough to warrant a clearer more understandable method which has an opportunity to be more efficient.

Like table.move, does not invoke any metamethods. Not that it would anyways, given that assigning nil to an index never invokes a metamethod.