From 8f37fc6dfc6972c21194fa4d159f98e0c919b847 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 14 Feb 2023 12:25:12 -0800 Subject: [PATCH] RFC: Deprecate table.getn/foreach/foreachi (#715) [Rendered](https://github.com/Roblox/luau/blob/zeux-rfc-deptable/rfcs/deprecate-table-getn-foreach.md) --- rfcs/deprecate-table-getn-foreach.md | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rfcs/deprecate-table-getn-foreach.md diff --git a/rfcs/deprecate-table-getn-foreach.md b/rfcs/deprecate-table-getn-foreach.md new file mode 100644 index 0000000..4ce9d46 --- /dev/null +++ b/rfcs/deprecate-table-getn-foreach.md @@ -0,0 +1,31 @@ +# Deprecate table.getn/foreach/foreachi + +## Summary + +Mark table.getn/foreach/foreachi as deprecated + +## Motivation + +`table.getn` was deprecated in Lua 5.1 that Luau is based on. + +`table.getn(x)` is equivalent to `rawlen(x)` when `x` is a table; when `x` is not a table, `table.getn` produces an error. It's difficult to imagine code where `table.getn(x)` is better than either `#x` (idiomatic) or `rawlen(x)` (fully compatible replacement). However, `table.getn` is slower and provides yet another way to perform an operation, leading new users of the language to use it unknowingly. + +`table.foreach` and `table.foreachi` were deprecated in Lua 5.2. + +`table.foreach` is equivalent to a `for .. pairs` loop; `table.foreachi` is equivalent to a `for .. ipairs` loop; both may also be replaced by generalized iteration. Both functions are significantly slower than equivalent `for` loop replacements, are more restrictive because the function can't yield, and result in new users (particularly coming from JS background) unknowingly using these thus producing non-idiomatic non-performant code. + +In both cases, the functions bring no value over other library or language alternatives, and thus just serve as a distraction. + +## Design + +We will mark all three functions as deprecated. The only consequence of this change is that the linter will start emitting warnings when they are used. + +Removing support for these functions doesn't provide any measurable value and as such is not planned in the foreseeable future because it may cause backwards compatibility issues. + +## Drawbacks + +None + +## Alternatives + +If we consider table.getn/etc as supported, we'd want to start optimizing their usage which gets particularly tricky with foreach and requires more compiler machinery than this is probably worth.