Hallo :-)
I have a suggestion/feature request for Lua 5.3 that I was hoping upstream Lua might take interest in.
It is my opinion that in languages like Python or Ruby it is convenient to be able to pollute the environment or at least create a slew of local definitions from a module with a function like import().
What I would like in 5.3 is the ability to do: import(io)
This would make local references to everything with a string key in the io table.
Another form would be: import(io, 'prefix_') or import(io, { 'write', 'read' }, 'prefix_')
Similar to table.insert(), the 2nd parameter (the prefix), becomes the 3rd parameter if a table specifying which values to localize is provided.
To me (my opinion), it looks odd and feels tedious to do things like:
local tinsert = table.insert
local tremove = table.remove
local tconcat = table.concat
local tsort = table.sort
Especially when you are localizing references from many tables...
[TO ME] it would look nicer to be able to do import(table, 't') and have all those locals defined. I want to create locals in batch and not simply modify the _ENV to "pass through" to the 'table' table (hah). Sometimes modifying _ENV is not an option or is just ~inappropriate~.
Unfortunately I cannot write this import() function I envision. It isn't possible to "insert locals" onto the stack from the Lua side of things and I have not looked into doing it from the C API (will do later). As I understand it after looking through the debug library (again), you cannot add more locals you can only replace them. I can't write an import() function that will add local references to the parent environment. I can do 'local a = thing.a' within import() but that reference is only in import() :]
I have thought about doing crazy things like creating dummy functions with the same number of locals as the table to import and then replacing them when I "jump" into that function environment by calling it -- but this is not a pretty concept.
I would LOVE it if upstream Lua would work their magic and find a way to insert locals in batch and provide an import() for us in Lua 5.3. This is not about "matching Python" or other languages, this is about reducing (((what I think most of us see))) as crufty boilerplate at the top of most Lua scripts. For 1 or 2 locals the original way is appropriate, but for localizing most of a module it would be nice to have this import()
Most of the time I just call table.insert() without localizing it, but sometimes I need to avoid the table lookup for efficiency. Sometimes there are so many local references it just looks icky... Sometimes we forget to use something we've localized or the code changed and we don't use it anymore. Scanning a list of "local thing = " is not as easy as reading: import(io, { 'read', 'write', 'setvbuf' })
TL;DR: Give me:
import(io) -- localize all string-key values in the io table
import(io, 'prefix_') -- ^ but make locals like prefix_read()
import(io, { 'just', 'these', 'keys' }) -- localize values associated with these keys only, skip the rest
import(io, { 'just', 'these', 'keys' }, 'prefix_') -- ^ prefix_just()
impl. notes:
-- you would not be able to localize values in tables where the key isn't a string
-- the prefix parameter shifts to the right like the value parameter in table.insert(), I thought this would feel more natural to the importer
Thoughts?
(please be constructive, I know this is a topic for bad criticism in some circles)