Skip to content

Some Lua pattern in Teal. #350

Answered by lenscas
nagolove asked this question in Q&A
Jan 20, 2021 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

every function that can be executed using that index need to have the same type, so what you can do is something like

local type CommandNames = enum
    "do1"
    "do2"
end

local commands : {CommandNames : function()} = { 
 do1 = function() print("do1")end,
 do2 = function() print("do2")end
}

function process(cmd: CommandNames)
    if cmd then
        local command: function() = commands[cmd]
        if command then
            command()
        end
    end
end

Basically, stop using a record and start using a map of commandName : yourFunctionType

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by hishamhm
Comment options

You must be logged in to vote
1 reply
@lenscas
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants