["all"] as an Lvalue?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

["all"] as an Lvalue?

Steve Litt
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?

Thanks,

SteveT

Steve Litt
March 2016 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Coda Highland
On Mon, Mar 28, 2016 at 5:55 PM, Steve Litt <[hidden email]> 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?
>
> Thanks,
>
> SteveT
>
> Steve Litt
> March 2016 featured book: Quit Joblessness: Start Your Own Business
> http://www.troubleshooters.com/startbiz
>

The [] is the syntax for using an expression for a table key. Some
people use it even for simple strings just so they don't have to have
something different if they have a key that happens to be a Lua
reserved word. (This is particularly relevant for configuration files
that might be modified by non-Lua-programmers.)

/s/ Adam

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Jonathan Goble
In reply to this post by Steve Litt
On Mon, Mar 28, 2016 at 8:55 PM, Steve Litt <[hidden email]> wrote:

> =====================================================
> 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?

No, the keys are ordinary string values. So the first one could also
be accessed as `domain_props.all`. The `[]` syntax here is just a way
to use an arbitrary expression as a table key; it's not needed for
"all", since that is a valid identifier, but it is necessary to use
the string "youtube.com" as a table key, since that string is not a
valid identifier.

Thus, this piece of code could also be written as:

domain_props = {
    all = {
        ["enable_scripts"]      = false,
        ["enable_plugins"]      = false,
        enable_private_browsing = false,
        user_stylesheet_uri     = "",
    },
    ["youtube.com"] = {
        enable_scripts = true,
        ["enable_plugins"] = true,
    },
}

Both domain_props tables, in your example and mine, are exactly identical.

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Daurnimator
In reply to this post by Steve Litt
On 29 March 2016 at 11:55, Steve Litt <[hidden email]> 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"}

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Hisham
On 28 March 2016 at 22:04, Daurnimator <[hidden email]> wrote:

> On 29 March 2016 at 11:55, Steve Litt <[hidden email]> 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"}

All three answers are correct, but if this was Stack Overflow yours
would be the one I would upvote, for being the clearest. :)

-- Hisham

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Steve Litt
In reply to this post by Daurnimator
On Tue, 29 Mar 2016 12:04:13 +1100
Daurnimator <[hidden email]> wrote:

> On 29 March 2016 at 11:55, Steve Litt <[hidden email]>
> 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"}
>

Thanks Coda, Jonathan and Daurnimator:

I'd seen that many times as states["florida"], but I'd never before
seen it as {["firstname"] = "steve"}. Now that you explained it, it
makes perfect sense and I got everything to work in Luakit.

Thanks!

SteveT

Steve Litt
March 2016 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz

Reply | Threaded
Open this post in threaded view
|

Re: ["all"] as an Lvalue?

Steve Litt
In reply to this post by Hisham
On Mon, 28 Mar 2016 23:10:18 -0300
Hisham <[hidden email]> wrote:

> On 28 March 2016 at 22:04, Daurnimator <[hidden email]> wrote:
> > On 29 March 2016 at 11:55, Steve Litt <[hidden email]>
> > 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"}  
>
> All three answers are correct, but if this was Stack Overflow yours
> would be the one I would upvote, for being the clearest. :)
>
> -- Hisham

Thanks Hisham!

Thanks to all the list's explanations, I got Luakit to work perfectly.

SteveT

Steve Litt
March 2016 featured book: Quit Joblessness: Start Your Own Business
http://www.troubleshooters.com/startbiz