[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Suggestion for 5.3: import() for creating a "slew" of locals
- From: Sean Conner <sean@...>
- Date: Mon, 18 Nov 2013 01:10:34 -0500
It was thus said that the Great Sir Pogsalot once stated:
> 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_')
In thinking about this further, it appears this topic keeps coming up
because a subset of programmers want to avoid excessive typing.
Okay, I can see wanting to avoid excessive typing of so called 'broiler
plate code', but there's more than one place to address this issue. Here's
a quick solution:
#!/usr/bin/env lua
mod = arg[1]
for i = 2,#arg do
print(string.format("local %s = %s.%s",arg[i],mod,arg[i]))
end
Name this script, say, "import", make it executable (according to the
manufactorer's instructions). I'm not sure what text editors are used, but
at least mine, I can hit ESC-!, then type
import table insert remove
And have
local insert = table.insert
local remove = table.remove
inserted where the cursor is. No changes to the language, minimal typing
required, and it makes explicit in the code what you are doing.
I'm not saying that script is perfect---obviously it can be modified to
suite styles and what not (like adding prefixes), but why are we burdening a
langauge with something that can easily be added by our tools?
-spc (I think the last non-programmable editor I used was the built-in
editor to the assembler on the TRS-80 Color Computer, and that was
over twenty-five years ago ... )