[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Undefined variables returning nil
- From: William Sumner <prestonsumner@...>
- Date: Tue, 19 Mar 2013 16:53:40 -0600
On Mar 19, 2013, at 4:40 PM, Kevin Martin <kev82@khn.org.uk> wrote:
>
> On 19 Mar 2013, at 22:32, William Sumner wrote:
>
>> I know Lua only has tables. Because they're used for arrays, classes, and more, catching only undefined globals is insufficient. I'm suggesting that, unless there's a good reason for the current behavior that I'm unaware of, relying on user-implemented solutions is undesirable and that the language should raise an error because the current default behavior is unsafe.
>
> By raising an error , you are removing the language's method of determining if a table contains a key (if table[key] == nil then … end). Surely you agree this is a fairly critical operation? How do suggest this is done instead?
>
> Kev
One solution might be to introduce an operator for determining if a table contains an element with a given key.
t = { a = "apple" }
if "a" in t then print "evaluates to true" end
if "b" in t then print "never prints" end
Another solution might be a table.contains() function:
t = { a = "apple" }
if t:contains "a" then print "evaluates to true" end
Preston