lua-users home
lua-l archive

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


On Wed, Sep 23, 2015 at 9:30 AM, John Hind <john.hind@zen.co.uk> wrote:
> t = {["cat"], ["dog"]}  -- Shortcut for t = {["cat"] = true, ["dog"] = true}
>
> I've always wanted to extend this to:
>
> t = {[cat],[dog]}
>
> So, similarly to keys for methods, you can drop the quotes if the string is
> a valid Lua name.

if the idea of the patch is that omitted values default to `true`,
then i'd say that the short form should be

t = { cat, dog }

because in normal Lua

t = { cat=true, dog=true }

is equivalent to

t = { ['cat']=true, ['dog']=true }

so if the `=true` part is optional, then you would be able to say either

t = { ['cat'], ['dog'] }

or

t = { cat, dog }

and the form {[cat], [dog]} would try to use the variables `cat` and `dog`




of course, i personally don't think that sets are special enough, and
usually just do

function Set(t)
    local o = {}
    for _, v in ipairs(t) do
        o[v] = true
    end
    return o
end

t = Set{'cat', 'dog'}

seems short enough and very readable


-- 
Javier