CS 3060: Programming Languages

Lua

Robert C. Green II, Ph.D.

Day 2

Indiana Jones

Tables

  • Where do I keep things when I need to access them by name?
  • Where do I store values in a particular order?
  • Fundmantally an Associative, Heterogeneous Array

Tables

                        
book = {
    title = "Grail Diary",
    author = "Henry Jones",
    pages = 100
}

book.title
book.author = "Henry Jones Sr."
book.stars = 5
                        
                    

Tables

                        
book.pages = nil
                        
                    

Tables

                        
function print_table(t)
    for k, v in pairs(t) do
        print(k .. ": " .. v)
    end
end
                        
                    

Arrays

                        
medals = {
    "gold",
    "silver",
    "bronze"
}                    
                    

Mix & Match

                        
ice_cream_scoops = {
    "vanilla",
    "chocolate";
    sprinkles = true
}
                   
                    

Metatables

                        
greek_numbers = {
    ena = "one",
    dyo = "two",
    tria = "three"
}

getmetatable(greek_numbers)
                        
                    

Metatables

                        
function table_to_string(t)
    local result = {}
    
    for k, v in pairs(t) do
        result[#result + 1] = k .. ": " .. v
    end
    return table.concat(result, "\n")
end
                        
                    

Metatables

                        
mt = {
    __tostring = table_to_string
}

setmetatable(greek_numbers, mt)

greek_numbers
                        
                    

OOP - Inheritance

http://lua-users.org/wiki/SimpleLuaClasses
http://lua-users.org/wiki/InheritanceTutorial

OOP - Multiple Inheritance

http://www.lua.org/pil/16.3.html

OOP - Privacy

http://www.lua.org/pil/16.4.html

Assignment

Continue the Assignment

Read Day 2

Resources

  • http://www.lua.org