This module feels redundant. Switch statements are meant to be cleaner if statements, and this just makes the code less readable. This is equivalent to adding a whole library to check if a number is even in Javascript
Totally valid take. For me, personally, I think there could be some nicer uses for it once the case fall throughs get added (really only time I use switches is when I’m seeking this functionality). Thanks for your comment ❤️
when switch cases are compiled in other languages it has a chance of being optimized in a way that is faster than the traditional if statement check depending on whats under the cases. Like equinox said it would've been better to add a break statement, would make it more useful
Very bad implementation ngl, if you want a switch in lua just make a table of functions and index it like this: the cases should be constant and global, which makes it have a time complexity of O(1) as they're only loaded once. if you want to switch on a string i suggest you hash it, this way there wont be any collisions with the default case cases = { [1] = function(FallThrough) print("Case 1"); end, [2] = function(FallThrough) print("Case 2"); return FallThrough(3); end, [3] = function(FallThrough) print("Case 3"); end, default = function(FallThrough) print("Default"); return FallThrough(4); end, [4] = function(FallThrough) print("Case 4"); return 69; end, [8] = function(FallThrough) print("Case 8"); end, } function doTableSwitch(value,cases) local case = cases[value]; local function FallThrough(case) return cases[case](FallThrough); end return (case or cases.default)(FallThrough); end print(doTableSwitch(2,cases));
might start using switches, because they seem very useful. I just need to look into them more so I have the full understanding of what they do. I think switches could improve my code a lot.
This isn't really a switch case I feel like the closest thing you could get to a switch case cleanly would be using a table of functions with the key as a case. This is only really more efficient than many ifelse if you have more than ~10 or so functions For a switch with no default local cases = { [1] = function() print("hi") end, [2] = function() print("hello") end, [3] = function(param) print(param) end, -- more here, just an example } If you are sure the case will be in the list run this local case = 2 cases[case]() -- prints "hello" if you want to use a default, do this case = 7 if cases[case] then cases[case]() else -- default is selected since case isn't in list print("default") end This is nice because you can also pass any arguments to the functions within cases and even get a return from them as well. It also can really speed up your code if you have many many cases in the function table.
What i dont like about this implementation is constructing the case at run time or within the code. An easier format: Local caseObj = CaseFactory:new( function(value) if value ~= 5 then return value end -- if you return a value it flows to the next case in the order. -- if you return a value and false then it will return a final value back at casecall and not go any farther in case values -if you return nothing it will stop there and not flow down. end, function(value) if value ~= 7 then return value end end ) Feels like the implemebtation is icer structured and since its done at run time vs pre defined and used i feel like that is problematic. Doing it this way makes it more readable, flexble and accounts for all the key controls you want. As well as allow q case to perform a transform and pass it through cases if you wanted as a result of. And ifnit was me i may include some quick preconstrucyed functions i vould pass in and fill out that would do the work for me.
This seems like it does the opposite of using an if statement. It's meant to be more readable, but it just makes the script it more cluttered. Not to mention that you would need to explain to people you're working with, why you're not using if statements or what each switch case is doing etc.
In post of making this video I've spoken with the person who made the module again and they'll be adding the case statement fall-through functionality soon! Hope you all are having a great week.
I know a trick that allows for cool syntax: local function switch(case) return function(cases) (cases[case] or cases.default)() end end switch (1) { [1] = function() print("One!") end, [2] = function() print("Two!") end, default = function() print("Something else!") end, }
I can see the use of this for long processes, this would be cleaner than a bunch of if statements, by just using tables u can have a more cleaner and smaller (maybe less readable) script. Simple Switch: (If continue is set to 0, the while loop will stop, otherwise make sure its 1) type SwitchValue = {Value: any, Function: callable, Continue: number} return function(Value: any, conditions: {[SwitchValue]: () -> (any)}) local x=#conditions-1; while x>-1 do if conditions[#conditions-x][1]>=Value then conditions[#conditions-x][2]() x *= conditions[#conditions-x][3] end x-=1 end end
for condition, callback in conditions do if (condition == value) and not returned then returned = callback() end end
if conditions.default and not returned then conditions.default() end
end end local MyValue = "hello" switch(MyValue){ hello = function() -- both hello and whats up dont return and so default follows through print("Hello !!!") end, whatsup = function() print("Whats up") end, bye = function() -- this does return so default doesnt follow through print("Goodbye :(") return true end, ["123"] = function() -- this is how it would work with numbers (the value passed is automatically converted to a string) print("beep boop") return true end, default = function() -- runs by default if no cases returned print("How are you") end, }
i have a better solution for that all u gotta do is just look for specific value in a table otherwise call default this aint that hard ```lua local function switch(value, tbl) local case = tbl[value] or tbl["default"]; if case then case(); else print("not able to cast the cases, consider using default case"); end end switch(123, { [123] = function () print("Hello, World!"); end, default = function () print("value not found"); end }); ``` to not make it redundant anymore i dont even know why would anybody use switches in lua u can just use tables
This module feels redundant. Switch statements are meant to be cleaner if statements, and this just makes the code less readable. This is equivalent to adding a whole library to check if a number is even in Javascript
Totally valid take. For me, personally, I think there could be some nicer uses for it once the case fall throughs get added (really only time I use switches is when I’m seeking this functionality). Thanks for your comment ❤️
Honestly such a useless implementation
when switch cases are compiled in other languages it has a chance of being optimized in a way that is faster than the traditional if statement check depending on whats under the cases. Like equinox said it would've been better to add a break statement, would make it more useful
Very bad implementation ngl, if you want a switch in lua just make a table of functions and index it like this: the cases should be constant and global, which makes it have a time complexity of O(1) as they're only loaded once. if you want to switch on a string i suggest you hash it, this way there wont be any collisions with the default case
cases = {
[1] = function(FallThrough)
print("Case 1");
end,
[2] = function(FallThrough)
print("Case 2");
return FallThrough(3);
end,
[3] = function(FallThrough)
print("Case 3");
end,
default = function(FallThrough)
print("Default");
return FallThrough(4);
end,
[4] = function(FallThrough)
print("Case 4");
return 69;
end,
[8] = function(FallThrough)
print("Case 8");
end,
}
function doTableSwitch(value,cases)
local case = cases[value];
local function FallThrough(case)
return cases[case](FallThrough);
end
return (case or cases.default)(FallThrough);
end
print(doTableSwitch(2,cases));
Nice
beautiful bro
A worse version of "If" statments
Switch cases are actually faster than else ifs if implemented correctly, this is just retarded ngl
This dude is underrated
Love 🙏
might start using switches, because they seem very useful. I just need to look into them more so I have the full understanding of what they do. I think switches could improve my code a lot.
i first learnt about switches when i first studied javascript
Love JavaScript!
This isn't really a switch case
I feel like the closest thing you could get to a switch case cleanly would be using a table of functions with the key as a case. This is only really more efficient than many ifelse if you have more than ~10 or so functions
For a switch with no default
local cases = {
[1] = function()
print("hi")
end,
[2] = function()
print("hello")
end,
[3] = function(param)
print(param)
end,
-- more here, just an example
}
If you are sure the case will be in the list run this
local case = 2
cases[case]() -- prints "hello"
if you want to use a default, do this
case = 7
if cases[case] then
cases[case]()
else -- default is selected since case isn't in list
print("default")
end
This is nice because you can also pass any arguments to the functions within cases and even get a return from them as well. It also can really speed up your code if you have many many cases in the function table.
This module is actually very interesting, if statements usually look pretty ugly especially with a large number of statements
I think it’s interesting as well!
Interesting
Oh man... Dude there is no shot that I wanted to make the exact same module 2 weeks ago!...
What i dont like about this implementation is constructing the case at run time or within the code.
An easier format:
Local caseObj = CaseFactory:new(
function(value) if value ~= 5 then return value end
-- if you return a value it flows to the next case in the order.
-- if you return a value and false then it will return a final value back at casecall and not go any farther in case values
-if you return nothing it will stop there and not flow down.
end,
function(value) if value ~= 7 then return value end
end
)
Feels like the implemebtation is icer structured and since its done at run time vs pre defined and used i feel like that is problematic.
Doing it this way makes it more readable, flexble and accounts for all the key controls you want. As well as allow q case to perform a transform and pass it through cases if you wanted as a result of.
And ifnit was me i may include some quick preconstrucyed functions i vould pass in and fill out that would do the work for me.
This seems like it does the opposite of using an if statement. It's meant to be more readable, but it just makes the script it more cluttered. Not to mention that you would need to explain to people you're working with, why you're not using if statements or what each switch case is doing etc.
In post of making this video I've spoken with the person who made the module again and they'll be adding the case statement fall-through functionality soon! Hope you all are having a great week.
I know a trick that allows for cool syntax:
local function switch(case)
return function(cases)
(cases[case] or cases.default)()
end
end
switch (1) {
[1] = function()
print("One!")
end,
[2] = function()
print("Two!")
end,
default = function()
print("Something else!")
end,
}
I can see the use of this for long processes, this would be cleaner than a bunch of if statements, by just using tables u can have a more cleaner and smaller (maybe less readable) script.
Simple Switch: (If continue is set to 0, the while loop will stop, otherwise make sure its 1)
type SwitchValue = {Value: any, Function: callable, Continue: number}
return function(Value: any, conditions: {[SwitchValue]: () -> (any)})
local x=#conditions-1;
while x>-1 do
if conditions[#conditions-x][1]>=Value then
conditions[#conditions-x][2]()
x *= conditions[#conditions-x][3]
end
x-=1
end
end
Simple Usage:
local switch = require(script.switch)
local MyInteger = 5
switch(
MyInteger,
{
{1,function() print(1) end,1};
{2,function() print(2) end,1};
{3,function() print(3) end,1};
{4,function() print(4) end,0};
{5,function() print(5) end,1};
{6,function() print(6) end,0};
{7,function() print(7) end,1};
{8,function() print(8) end,1};
{9,function() print(9) end,0};
}
)
this is my take on switch cases in luau, its not perfect but its as close as I could get it
local switch = function(value: any)
local returned = false
value = tostring(value)
return function(conditions: {[string]: ()->(any)})
for condition, callback in conditions do
if (condition == value) and not returned then
returned = callback()
end
end
if conditions.default and not returned then
conditions.default()
end
end
end
local MyValue = "hello"
switch(MyValue){
hello = function() -- both hello and whats up dont return and so default follows through
print("Hello !!!")
end,
whatsup = function()
print("Whats up")
end,
bye = function() -- this does return so default doesnt follow through
print("Goodbye :(")
return true
end,
["123"] = function() -- this is how it would work with numbers (the value passed is automatically converted to a string)
print("beep boop")
return true
end,
default = function() -- runs by default if no cases returned
print("How are you")
end,
}
slow, don't do a for loop
@@alminecrafteano right idk how i didn't notice that
just use **if** this is overcomplicated
what's your vscode theme?
i have a better solution for that
all u gotta do is just look for specific value in a table otherwise call default this aint that hard
```lua
local function switch(value, tbl)
local case = tbl[value] or tbl["default"];
if case then
case();
else
print("not able to cast the cases, consider using default case");
end
end
switch(123, {
[123] = function ()
print("Hello, World!");
end,
default = function ()
print("value not found");
end
});
```
to not make it redundant anymore
i dont even know why would anybody use switches in lua u can just use tables
I learned about switches when i used java
Java 🙏🙏
Hello, New upload? Dont mind if i do!
Hey Mono!
@@summerequinox Hello! I'm working a lot on that module!
using if else for substituting luau not having switch makes me feel like Yandere Dev :(
using switches in lua is just stupid because there arent switches in lua normally so its alot worse + its alot slower
You're a serial killer for using vs code for roblox
Lmaooooo
Somehow made switch case OOP... these lua devs man
For real
@@OcnarfPro shut up ocnarf
it seems pointless