Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This function attempts to emulate switch statements used in programming languages such as C++.
- -- First argument is the case in the statement to run. This is a value to index an element in a table. Indexes can be any number or string.
- -- Second argument is a table containing functions representing each case. This only executes the case indexed by the first argument. Setting a case equal to a number or string represents a pointer to a different case function. This is to avoid copy and pasting functions. Pointers cannot point to pointers. Unlike C++ switch statements, these switch statements can be nested within switch statements.
- -- Third argument is the default case. This is run if none of the other cases are executed.
- -- Additional arguments are passed to the case functions.
- switch = function(case, cases, defCase, ...)
- if cases[case] then
- if type(cases[case]) == "function" then return cases[case](...)
- elseif type(cases[case]) == "number" or type(cases[case]) == "string" then return cases[cases[case] ](...)
- else error("Case element must be a number, string, or function.", 2) end
- else
- if defCase then return defCase(...)
- else error("Undefined case '"..tostring(case).."'.", 2) end
- end
- end
- -- Examples of the switch statement in use.
- local function testProgram()
- for a = 0, 5 do
- switch(a, {
- [0] = function()
- print('h')
- end,
- [1] = function()
- print('e')
- end,
- [2] = function()
- print('l')
- end,
- [3] = 2, -- This case points to case [2], so case [3] will run the function for case [2]. Another case may not point to case [3], or else the program will throw an error.
- [4] = function()
- print('o')
- end
- }, function(punctuation)
- print(punctuation)
- end, '.')
- end
- switch("pear", {
- ["apple"] = function()
- print("Hello world")
- end,
- pear = function()
- print("Switch statements are neat.")
- end
- }, function() end)
- switch("apple", {
- ["fruit"] = function()
- print("Hello world")
- end,
- veggie = function()
- print("Switch statements are neat.")
- end,
- apple = "fruit"
- }, function() end)
- end
- -- Uncomment the line below to run the test program function.
- -- testProgram()
- -- This program was written for ComputerCraft, but I have not used any functions that are not standard to Lua, so it should work with any Lua application.
- -- This program was tested in CCEmuX which is a ComputerCraft "emulator" so that I don't have to start up all of Minecraft every time I want to make something in CC that's not explicitly Minecraft related.
- -- You can use this with ComputerCraft's os.loadAPI() to load the Class() function into a separate program file.
- -- Alternatively, you can use the require() function, which does effectively the same thing but is newer to ComputerCraft.
- -- If you can I highly recommend using require() instead because it's way more efficient, and it's a standard Lua function so it should work beyond ComputerCraft, but I have not tested this to be sure.
- --[[ Changelog
- 2022/06/22: This switch statement will be deprecated for performance reasons. I realized that this is actually 2 times slower than regular if statements.
- 2022/03/03:
- If a default case is not defined and the given case is also not defined, an error will be thrown for the invalid case.
- 2021/07/28:
- Initial release.
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement