lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You know all those epiphanies you have right after you've sent the giant email to a mailing list?  Here's the addendum:

- localize() instead of import() so it doesn't get confused as having some function like require()
- I'm not saying let's get rid of all the 'local a = module.a' lines, just provide this for when you need to create a lot of locals

My reasoning for all this is that sometimes all the table.*() or string.*() stuff looks like "extra" on your screen.  I mean people see insert() and usually make the mental connection that it's table.insert().  Part of the reason string parsing is so easy to do in Perl is that you have all those global functions like chomp() and split().  It just makes sense to bring those functions "closer" with local references when you know you're going to be using them a lot.  Polluting the global env is a no-go, but making more locals works :]  I'm just trying to suggest something that would allow doing this in batch for convenience/readability.

Extra things to consider:

- functions in lua have 'local limit' of 200 locals per function: http://www.lua.org/source/5.2/lparser.c.html#MAXVARS

I'm still trying to find the place in the C source that lets 'local a = module.a' work...  This should be possible to do from C.  I was just hoping it could be something in 5.3 also :>


On Sun, Nov 17, 2013 at 7:46 AM, Sir Pogsalot <sir.pogsalot@gmail.com> wrote:
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)