Advertisement
Mangus875

Lua Switch Statement

Mar 31st, 2023 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. -- NOTE: This function does not currently work with tables however I plan on adding that in the near future.
  2.  
  3. local function switch(val, ...)
  4.     local allowedTypes = {
  5.         "number",
  6.         "string"
  7.     }
  8.     local allow = false
  9.     for _, t in pairs(allowedTypes) do
  10.         if typeof(val) == t then
  11.             allow = true
  12.             break
  13.         end
  14.     end
  15.     if not allow then
  16.         error("Invalid switch variable type")
  17.     end
  18.     ---
  19.     local cases = {...}
  20.     local default
  21.     if type(cases[#cases]) == 'function' then
  22.         default = cases[#cases]
  23.         table.remove(cases, #cases)
  24.     end
  25.    
  26.     local caseVals = {}
  27.     for _, case in pairs(cases) do
  28.         table.insert(caseVals, case[1])
  29.     end
  30.     for i1, v1 in pairs(caseVals) do
  31.         for i2, v2 in pairs(caseVals) do
  32.             if v1 == v2 and i1 ~= i2 then
  33.                 error("Duplicate case error: case #"..i2.." is a duplicate of case #"..i1)
  34.             end
  35.         end
  36.     end
  37.    
  38.     for _, case in pairs(cases) do
  39.         if val == case[1] then
  40.             return case[2]()
  41.         end
  42.     end
  43.     if default then
  44.         return default()
  45.     end
  46. end
  47.  
  48. -- USAGE EXAMPLE:
  49. local nutrition
  50. local food = "Pizza"
  51. switch(food,
  52. {"Fruit",
  53.     function()
  54.         nutrition = 100
  55.     end
  56. },
  57. {"Beef",
  58.     function()
  59.         nutrition = 75
  60.     end
  61. },
  62. {"Pizza",
  63.     function()
  64.         nutrition = 40
  65.     end
  66. },
  67. {"Candy",
  68.     function()
  69.         nutrition = 10
  70.     end
  71. },
  72. {"Salad",
  73.     function()
  74.         nutrition = 95
  75.     end
  76. },
  77. function()
  78.     nutrition = 50
  79. end)
  80. print(food.." has a nutritional value of "..nutrition)
  81.  
  82. --[[    Processing/Java code translation
  83. int nutrition;
  84. String food = "Pizza";
  85.  
  86. switch(food) {
  87.     case "Fruit":
  88.         nutrition = 100;
  89.         break;
  90.  
  91.     case "Beef":
  92.         nutrition = 75;
  93.         break;
  94.  
  95.     case "Pizza":
  96.         nutrition = 40;
  97.         break;
  98.  
  99.     case "Candy":
  100.         nutrition = 10;
  101.         break;
  102.  
  103.     case "Salad":
  104.         nutrition = 95;
  105.         break;
  106.  
  107.     default:
  108.         nutrition = 50;
  109.         break;
  110. }
  111. println(food + " has a nutritional value of " + nutrition);
  112. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement