1

This seems to be legitimate Lua syntax:

example = { ["dummy"] = "foobar"}

Why would you use the above and not simply:

example = { dummy = "foobar"}

They appear to functionally the same...

4
  • It was surprising for me that I can't write, for example, t = { in=0, out=1, err=2 } Commented Nov 18, 2016 at 22:58
  • Surprising that you can't write? @egorskriptunoff it looks valid to me Commented Nov 19, 2016 at 13:39
  • 1
    @warspyking in is a keyword, so it can't be used as a table field name without brackets. Commented Nov 19, 2016 at 16:36
  • Oh LOL totally didn't see that @colonel Commented Nov 20, 2016 at 3:36

1 Answer 1

1

Because field names do not have to be identifiers: they can be any string, including strings having spaces, for instance.

The second statement is valid Lua syntax and is very convenient for field names that are identifiers.

Table keys do not have to be strings: they can actually be any value (except nil) and the [expr]=expr syntax for table entries allows the values of arbitrary expressions to be used as keys.

Sign up to request clarification or add additional context in comments.

2 Comments

Can they be any value except nil? I'm pretty sure there's another one.
except nil and nan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.