lua-users home
lua-l archive

Re: ["all"] as an Lvalue?

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


On 29 March 2016 at 11:55, Steve Litt <slitt@troubleshooters.com> wrote:
> Hi all,
>
> I'm writing a document on Luakit, and in my section on making fonts
> bigger, I need to give instructions to edit the domain_props table,
> which looks something like this:
>
> =====================================================
> domain_props = {
> ["all"] = {
> enable_scripts = false,
> enable_plugins = false,
> enable_private_browsing = false,
> user_stylesheet_uri = "",
> },
> ["youtube.com"] = {
> enable_scripts = true,
> enable_plugins = true,
> },
> }
> =====================================================
>
> It looks like domain_props is a table whose two elements are each
> tables, named ["all"] and ["youtube.com"] respectively. I've never seen
> something like ["all"] being the name of a table element or a variable
> name before. What's going on, is ["all"] an anonymous table containing
> element "all"? I just don't understand this syntax, and why somebody
> would do this. What am I missing?
{ foo = "bar" }
is actually just short for:
{ ["foo"] = "bar" }
The first shorter form however only works for keys that are valid identifiers.
Which means you have to use the 2nd form for:
 - keywords (e.g. {["end"] = 1234})
 - non-valid identifiers
 - e.g. starting with a number: {["1thing"] = item}
 - e.g. containing a ".": {["youtube.com"] = "a website"}
 - non-string keys. e.g. {[50] = "number as key"}

AltStyle によって変換されたページ (->オリジナル) /