Curly braces?

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
Have you seen this before? I noticed in the patch they changed this:

Code:
for building in GameInfo.Buildings() do
    local buildingID = building.ID;
    if building.SpecialistType == pSpecialistInfo.Type then
        if (pCity:IsHasBuilding(buildingID)) then
            iGPPChange = iGPPChange + building.GreatPeopleRateChange * 100;
        end
    end
end

to this...
Code:
for building in GameInfo.Buildings{SpecialistType = pSpecialistInfo.Type} do
    local buildingID = building.ID;
    if (pCity:IsHasBuilding(buildingID)) then
        iGPPChange = iGPPChange + building.GreatPeopleRateChange * 100;
    end
end

Now... I know the "for x in y()" executes a sql select statement on the database, and normally if we do GameInfo.Buildings("SpecialistType = '" .. pSpecialistInfo.Type .. "'") the part in the parenthesis is the WHERE clause. I've never seen this curly brace format before, but I'm assuming it does the same thing with less typing hassle... can anyone confirm this?
 
It isn't standard LUA syntax that I'm aware of, probably some trickery with the metatable.

It seems what it is probably doing is calling a function with a table as the argument. The function then probably iterates over all the rows and matches the columns in the database to the attributes in the passed table. If this is the case, you would only be able to use this format for equality of multiple columns and it would likely parse as a syntax error if you tried to put SQL in between the bracers.
 
There are certainly no other lexical possibilities in the Lua language definition than a table constructor, and I find the following in the language spec:

Code:
functioncall ::=  prefixexp args | prefixexp `:´ Name args 
args ::=  `(´ [explist] `)´ | tableconstructor | String

Indicating that a table constructor (some folks I've heard call it a table literal) or a string literal are as valid as (blah,blah,blah), so yes, it's calling it as a function with that table as the sole argument. I assume the logic is as you suggest.

I was not previously aware of this syntaxt - sounds quite handy. Would look weird using the string option, though.
 
I've seen the string option used with the print statement in a few locations in the "automation" lua files, and in CivilopediaScreen.lua there's this twice:

print "---------------------------------"
 
Back
Top Bottom