While I’ll like this, im curious why you need a module for something that can be trivilised with a dictionary of functions, this is functionally the same and has less boilerplate code
local yardSale = {
table = function()
print("£100")
end,
chair = function()
print("£25")
end
default = function()
print("something else")
end
}
(yardSale[yardSaleItems] or yardSale.default)()
I explain this in the video, but that is actually something that is the best solution at times. However, at other times, switch statements make more sense.
Switch statements allow you to provide the same output with different conditions.
I recommend you give that video a watch to better understand what I’m saying!
Ooh, not a bad idea, I will definitely have to try and implement that soon.
Cases and default statements are their own userdata components, as opposed to be indexes. In other words, this means that it becomes a lot easier to store them in their own locations (such as separate modules).
Also, you can easily store different conditions for the same outcome. In their implementation, that does not exist.
There is a very good reason why you shouldn’t be trying to recreate pseudo-switch syntax in Luau like this. Passing lambda functions to evaluate the conditioning makes the overall computation about 26875 times slower in your example above!
Here’s the benchmarks I did with aggressive optimizations enabled:
Switch
--!optimize 2
local start: number = os.clock()
local switch, case, default = unpack(require(script.Switch))
local yardSaleItem: string = "shoes"
switch(yardSaleItem){
case("table")(function()
print("$100")
end);
case("chair")(function()
print("$25")
end);
case("toy")();
case("plate")();
case("shoes")();
case("clothing")(function()
print("$5")
end);
default()(function()
print("something else")
end)
}
print(os.clock() - start)
Normal If-Statement
--!optimize 2
local start: number = os.clock()
local yardSaleItem: string = "shoes"
local function checkItem(): ()
if yardSaleItem == "table" then
print("$100")
return
end
if yardSaleItem == "chair" then
print("$25")
return
end
if yardSaleItem == "toy" or yardSaleItem == "plate" or yardSaleItem == "shoes" then
print("$5")
return
end
-- else
print("something else")
end
checkItem()
print(os.clock() - start)
Outcomes
Switch: ~2.15 μs
Normal If-Statement: ~0.00008 μs
Yes, it’s easier to read and understand syntax, I get that. But just like micro-optimizations, there’s a point when too much performance gets lost in readability and you need to cut back.
Adding to optimization issues, there is an effortless way to emulate C-style switches as stated below:
Within this post, I would instead state the syntax of this is similar to C++'s because switches are way different than anyone would think, and the following states why (if you’d like to review them).
C++ Switches
Fall-throughs occur since they are not broken out of:
However, I wouldn’t really recommend the use of this as well as other “switch” emulator modules. They can be slow and you really do not want that within code (high performance), especially with games. It could be used with aesthetics though I do like the “Multiple cases evaluates to one output” reference.
Anyways, If you were to create a “switch” with speed and efficiency I would recommend metatablecat’s method (given above) as it’s much faster. It produces way less bytecode than the implementation of the module also. This is a nice reference and module but unsure of its use of it within the production of in-game code.
local module = {}
module[2] = {Default="default"} -- lazy to style it
table.insert(module, function(var)
return function(cases)
if cases[var] then
cases[var]()
elseif cases["default"] then
cases["default"]()
end
end
end)
local switch, symbols = table.unpack(module)
switch("davre"){
Darve = function()
end,
dave = function()
print("hi")
end,
[symbols.Default] = function()
print("no switches found")
end
}