Shibalicious
Shibalicious

Reputation: 288

Lua unusual variable name (question mark variable)

I have stumbled upon this line of code and I am not sure what the [ ? ] part represents (my guess is it's a sort of a wildcard but I searched it for a while and couldn't find anything):

['?'] = function() return is_canadian and "eh" or "" end

I understand that RHS is a functional ternary operator. I am curious about the LHS and what it actually is.

Edit: reference (2nd example):

http://lua-users.org/wiki/SwitchStatement

Upvotes: 1

Views: 1323

Answers (3)

Piglet
Piglet

Reputation: 28950

The other posts alreay explained what that code does, so let me explain why it needs to be written that way.

['?'] = function() return is_canadian and "eh" or "" end is embedded in {}

It is part of a table constructor and assigns a function value to the string key '?'

local tbl = {a = 1} is syntactic sugar for local tbl = {['a'] = 1} or

local tbl = {}
tbl['a'] = 1

String keys that allow that convenient syntax must follow Lua's lexical conventions and hence may only contain letters, digits and underscore. They must not start with a digit.

So local a = {? = 1} is not possible. It will cause a syntax error unexpected symbol near '?' Therefor you have to explicitly provide a string value in square brackets as in local a = {['?'] = 1}

they gave each table element its own line

local a = {
    1,
    2,
    3
}

This greatly improves readability for long table elements or very long tables and allows you maintain a maximum line length.

You'll agree that

local tbl = {
    z     = function() return is_canadian and "zed" or "zee" end,
    ['?'] = function() return is_canadian and "eh" or "" end
}

looks a lot cleaner than

local tbl = {z = function() return is_canadian and "zed" or "zee" end,['?'] = function() return is_canadian and "eh" or "" end}

Upvotes: 2

Robert
Robert

Reputation: 2812

Actually, it is quite simple.

  local t = {
    a = "aah",
    b = "bee",
    c = "see",

It maps each letter to a sound pronunciation. Here, a need to be pronounced aah and b need to be pronounced bee and so on. Some letters have a different pronunciation if in american english or canadian english. So not every letter can be mapped to a single sound.

z = function() return is_canadian and "zed" or "zee" end,
['?'] = function() return is_canadian and "eh" or "" end

In the mapping, the letter z and the letter ? have a different prononciation in american english or canadian english. When the program will try to get the prononciation of '?', it will calls a function to check whether the user want to use canadian english or another english and the function will returns either zed or zee.

Finally, the 2 following notations have the same meaning:

  local t1 = {
    a     = "aah",
    b     = "bee",
    ["?"] = "bee"
  }

  local t2 = {
    ["a"] = "aah",
    ["b"] = "bee",
    ["?"] = "bee"
  }

Upvotes: 3

luther
luther

Reputation: 5554

If you look closely at the code linked in the question, you'll see that this line is part of a table constructor (the part inside {}). It is not a full statement on its own. As mentioned in the comments, it would be a syntax error outside of a table constructor. ['?'] is simply a string key.

Upvotes: 4

Related Questions