lua-users home
lua-l archive

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


> I'd only say that the crazy amount of "a, b, c = a, b, c" is rather hard to explain as a "sweet feature of Lua"

I don't mind writing local a,b,c = tbl.a, tbl.b, tbl.c, but what I
sometimes want is to be able to say:

local insert, remove, concat alias table

This would assign insert, remove, and concat from "table" with a small
twist: when table.insert changes, that change is propagated to the
local variable "insert". I'm less certain how this should work when
the entire table "table" changes, but for this to work in "require,
unrequire, require" scenario (which is what I'd want), this would need
to work when a new table is assigned as well.

The primary use for me would be to be able to re-load modules with the
changes "seen" in their dependent modules (even when localized if
using this "alias" mechanism), but there may be other use cases.

Maybe that's an overkill, and what I really want is something like this:

local mymodule = proxy(require("mymodule"))

with "proxy" doing some magic that changes "mymodule" variable when
the table returned by "require" changes (similar to what described
above).

Paul.

On Sun, Nov 17, 2013 at 8:48 AM, Andrew Starks <andrew.starks@trms.com> wrote:
>
>
> On Sunday, November 17, 2013, Sir Pogsalot wrote:
>>
>> I thought I would add the code I envisioned for from(), which probably
>> matches something on the lua-users wiki but I can't find the page...
>>
>> local from =
>>     function (t, ...)
>>         local collected = {}
>>
>>         for _, k in ipairs({ ... }) do
>>             table.insert(collected, t[k])
>>         end
>>
>>         return table.unpack(collected)
>>     end
>>
>> local tins, trem, tcat = from(table, 'insert', 'remove', 'concat')
>>
>> The Power Patches page is cool ;]
>>
>> PS:  Just want to leave a note saying that I don't like from() -- as I
>> said earlier, it encourages users to make smaller identifiers so they don't
>> have to write each one twice (once after local and again in from)
>>
>>
>>
>> On Sun, Nov 17, 2013 at 9:03 AM, Paige DePol <lual@serfnet.org> wrote:
>>>
>>> Rena, you should check out the "Unpack Tables by Name" patch over on the
>>> Power Patches page (http://lua-users.org/wiki/LuaPowerPatches)
>>>
>>> I have not patched my Lua with this patch yet, however, it looks like it
>>> would do exactly what you desire; namely let you assign locals to the result
>>> of table key lookups in a concise manner!
>>>
>>> >From the Power Patches page:
>>>
>>> Enhancement to the assignment statement to unpack named values from
>>> tables using the in keyword:
>>>     local a, b, c in some_table_expression
>>>
>>> Is syntactic sugar for:
>>>     local t = some_table_expression
>>>     local a, b, c = t.a, t.b, t.c
>>>
>>> ~pmd~
>>>
>>>
>>>
>>> On Nov 17, 2013, at 2:37 AM, Rena <hyperhacker@gmail.com> wrote:
>>>
>>> > But the point to understand is that this must be a purely _static_
>>> > operation at compile time. So implementing something like 'using
>>> > table.*' hits the problem of determining the contents of 'table' at
>>> > compile time.  So it would have to be an explicit list of entries to
>>> > localize, and understood as syntactical sugar for all those pesky
>>> > 'local insert = table.insert' statements.
>>>
>>>
>>
>
> I don't know if the application would be considered wide enough but:
>
> local * = t.a, t.b, t.c
>
> I'm not sure if I'd advocate for:
>
> local * = t.*
>
> That seems really unlua (doing something in one line that takes a loop)
>
> I use _ENV and pl for this stuff and on the boheamoth hardware that I use,
> the speed doesn't make a difference.
>
> I'd only say that the crazy amount of "a, b, c = a, b, c" is rather hard to
> explain as a "sweet feature of Lua"
>
> Not on my top 10, but my vote is that some subtle amount of sugar would be
> welcome.
>
> -Andrew