I look at some peoples code, and I often will see many using [“foo”] in a dictionary as a key, which has always left me wondering, why not just use foo on its own instead. Are there any performance impacts, or is it simply preference?
example
local fruit = {
apple = "Yummy";
banana = "Better";
};
vs
local fruit = {
["apple"] = "Yummy";
["banana"] = "Better";
};
I get what you mean now, there’s no performance impacts as far as I’m aware, some may consider using square brackets to allow for spaces within string indexes (any other reason is obvious, e.g number indexes can’t be defined without brackets) that’s all, or any other reason concerning preference.
The way you define your indexes isn’t consequential whatsoever to how they’re handled.
If your key is a number_key, say someone’s userid, a string key will do over a key
local userid = consider:gen("id", 12) -- Returns a random int from the userid min to 10
local x = {
82832923 = userid -- error
}
local z = {
["82832923"] = safe
}
This will actually work if you put it in brackets, it doesn’t need to be a string. [82832923] = userid
Actually, any variable or value can be a key so long as you put it inside of brackets. This allows you to create some unique shortcuts if you’re clever.