lua-users home
lua-l archive

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





On Sun, May 18, 2014 at 10:23 AM, Thiago L. <fakedme@gmail.com> wrote:

On 18/05/2014 11:54, Thomas Jericke wrote:

-----Original Message-----
From: "Philipp Janda" <siffiejoe@gmx.net>
To: lua-l@lists.lua.org
Date: 18-05-2014 16:01
Subject: Re: Long chains of objects (aka tables)

What kind of data structures are you guys dealing with? The longest
sequence of fixed length with constant table lookups in my code is
`package.loaded.math`

-Andrew

Philipp


Any structure that is more of a tree than a namespace structure will easily a depth of 5 or more.

Think of stuff like DOM in _javascript_:
document.body.style.backgroundColor = "blue"

In our case we speak about machines:
Machine.Modules.Portal.Commands.MoveX(24)

if Machine.Modules.Portal.Properties.Width > 20
   ...
end


Agreed. This is the use case that I run against using tables for documents / schema processing, not as much for data.

 
 

We have lots of code that looks exactly like that. But most of the time,
raising an error is perfectly the right thing to do.

The ? operator looks interesting to me, but I think it would need to be more general.
Using it only for table lookups looks to limited to me.

I mean, what about things like:
X.Y?()
local res = X? + Z?
(res is nil if x is nil or z is nil)

And finally I ask myself if a similar syntax could possibly solve the default
value problem.

function do(boolean)
   local boolean? = true -- assigns true if boolean is nil.
end

Remember "local boolean = boolean or true" does not the right thing!

A left hand side ? would break on not nil. So doing the opposite of the
right hand side. Well that may be confusing, so maybe we finally get a ¿ operator?

Disclaimer: I am just thinking out loud, this are neither suggestions nor proposals.
--
Thomas





So you want ? to make a fake nil with a metatable which returns nil whatever you do?

Also have you tried: boolean = unpack(boolean ~= nil and {boolean} or {true})



There is no problem being solved. Lua is fine. We are talking about sugar. 

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

```

All alternatives presented thus far are less clear than either option. The present syntax only suffers from being extremely verbose (and in the above example, 3 local lookups and 9 field lookups vs 1 / 3 for the success case) and thus arguably less clear.

-Andrew