lua-users home
lua-l archive

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

I want to thank Petite Abeille for being the most contributory, the 2nd approach was creative.  I would probably stick with the from() I posted, but I thought your code was really thought-provoking and I appreciate you putting more code to this thread. :]

Most of the replies boil down to: 
1) don't add more syntactic sugar
2) add the 'local a, b, c in some_module' patch to 5.3
3) modify _ENV
4) have an external script generate the 'local a = mod.a' list
5) "why does this idea keep coming up?"
6) would encourage "fat" bytecode and shadowing problems

My response:
1) I didn't suggest adding syntactic sugar, I am not proposing that
2) I do like that patch but ^
    (also, local a, b, c in modname removes the ability to add a prefix to the identifiers)
3) I said in my original email that modifying _ENV is not always a good idea
4) Running an external script to generate the preamble sounds trendy...
5) There must be some reason around here...
6) This one has some merit, but I'd rather let users shoot themselves. :3

Just want to add that I use Lua because of its simple grammar.  The performance and small distribution size is definitely nice, but what I like most is how I can show Lua to people who don't know Lua and it's pretty easy for them to understand what's happening.  I don't get that with Python or Ruby or Perl...  especially Perl.  My point being: I love Lua for being a very "clear" language.  Most of the pseudocode examples resemble Lua and that's a great thing.  It's not as concise as Python, but it does save keystrokes compared to languages like PHP or Perl.  I use Lua because it's simple to understand and it does save me keystrokes.  The problem is these local lines.  I see a lot of them and they're not easy to 'skim'.  It makes it difficult to review others' code if you're making sure they use everything they've referenced.  Most people don't even do it to avoid table lookup times, it's just to avoid writing "string." or "table." everywhere.  It's both a conveniency issue and a readability issue (imo).  The could be better but *exactly* what I want doesn't sound possible with how locals are currently allocated at compile time.

Things to remember:
- I'm trying to avoid messing with _ENV
- I'm trying to create locals in batch
- I want to create locals [not globals!!!] with import()
- I want the ability to provide a prefix to the generated local identifiers
- I want to be able to specify a list of what to localize from the table

Anyway, I'm outtie. :]

On Mon, Nov 18, 2013 at 6:10 AM, Sean Conner <> wrote:
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]))

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 ... )