[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Surprising behavior of table.insert()
- From: Hisham <hisham.hm@...>
- Date: Sat, 26 Feb 2011 16:42:44 -0300
On Fri, Feb 25, 2011 at 5:52 PM, Axel Kittenberger <axkibe@gmail.com> wrote:
> There is also a difference betwen
> function f() return end
> and
> function f() return nil end
Interesting! I had never occured to me that there is actually a
semantic difference between "return" and "return nil" in Lua! Over the
years I've been using "return nil" as a sort of documentation, as in
"this function usually returns a value, but for this path it returns
nothing".
On Fri, Feb 25, 2011 at 5:35 PM, Javier Guerra Giraldez
<javier@guerrag.com> wrote:
> if you return at most one value, just wrap your function call in parenthesis.
>
> table.insert (t,(f()))
Didn't think of that either. I tend to read explicit parentheses as
"keep the first result and ignore the rest" -- I'll keep in mind that
the meaning is more subtle than that.
As for the other suggestions, at first I did consider writing
table.insert(t, f() or nil) like Enrico suggested (my function never
returns false) but that felt a bit obfuscated (the purpose of the "nil
or nil" hack would be an unnecessary mental speed-bump for someone
else reading the code). What I ended up doing was the same as Steve
Litt suggested: assigning to a temporary variable.
Thanks to everyone for your feedback! I learned new things in this thread. :)
-- Hisham