lua-users home
lua-l archive

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





On Sun, May 18, 2014 at 1:52 PM, Jay Carlson <nop@nop.com> wrote:

On May 18, 2014 1:07 PM, "Andrew Starks" <andrew.starks@trms.com> wrote:

> On Sunday, May 18, 2014, Jay Carlson <nop@nop.com> wrote:
>>
>> On May 18, 2014 12:05 PM, "Andrew Starks" <andrew.starks@trms.com> wrote:
>>
>> > However, if anything were to be done, it must make things clearer. So, which is clearer to you:
>> >
>> > ```
>> > --Do we have have an item, with a background color?
>> > if not (document.body.item
>> >     and document.body.item.style
>> >     and document.body.item.style.backgroundColor) then
>> >     ....
>> > end
>> >
>> >
>> > --or (accepting alternate forms of the OP's style (|?.| or what have you)
>> >
>> > if not document.body?item?style?backgroundColor then
>> >    ...
>> >
>> > end
>>
>> What's inside the body of the "if"?
>
>
> Don't set the background color? The example is completely contrived. If the use cases aren't obvious, then it's fair to ask for real examples. But, I'm not sure I follow the question.  

I ask because I rarely see deeply nested tests of attribute presence without making use of the the path navigating to the attribute. Without written-out examples I worry this is trying to write:

    if not document.body?item?style?backgroundColor then
        return
    end
    repaint(document.body.item.style.backgroundColor)

Better to write:

    local bg = document.body?item?style?backgroundColor
    if not bg then
        return
    end
    repaint(bg)

Jay

I started to write an lpeg parser to go through all of my code and check for examples of a.b.c and a.b.c.d ... Then I played around with Cmt, got that going... then I had to go to work. :)

The reason that Dirk's solutions doesn't work for me is that this just does not come up all that often ( .1 times a source file?) and quoting things or namespace collisions in _ENV are not a thing that I want to think about[1]. 

When I do need them, I feel sad, but it's just not that big of a deal. It goes in the bucket of "+=" or "++" and I go on my merry way.

The only reason I voice support for the idea is that some variation of "a.b.c?d" seems clearer to me and I like clearer. 

Achieving that syntax isn't worth a hack and so I'll just do things the "normal" way. :)

-Andrew

[1] With the advent of _ENV, using the "global" environment for real work is not unheard of, for me.