On 16/05/2014 20:18, Andrew Starks
wrote:
On Friday, May 16, 2014, Thiago L. <fakedme@gmail.com> wrote:
So I did some
benchmarking: https://gist.github.com/SoniEx2/4b1d9d8e6427cf5a1439
It seems like pcall is almost the best way to do Groovy's ?.
stuff in Lua: http://groovy.codehaus.org/Null+Object+Pattern
(let me quote it)
"with the |?.| operator, an _expression_ like |p?.job?.salary|
will be equal to null if |salary| is equal to null, or if |job|
is equal to null or if |p| is equal to null. You don't need to
code a complex nested |if ... then ... else| to avoid a
|NullPointerException|."
(actually pcall is more like catching a NullPointerException but
w/e)
Now here's what I'm curious about: how fast would indexing long
chains be if Lua had the ?. operator? (so that (nil)?.whatever
== nil and someObject?.whatever == someObject.whatever)
(Yes that does mean someone'll have to patch Lua to support ?.,
and yes they'll have to run all the stuff I did on my benchmark,
and yes my benchmark seems to cause a glitch...)
Test it yourself:
debug.setmetable(nil, { __index = nil})
Then the `.` operator will behave like ?
I'm curious for the results.
-Andrew
Well it seems to be WAY faster... It would be nice to see it in
vanilla Lua...
|