Group comparison values

Currently if you want to compare one value to multiple other values, you need to either group if statements or iterate over an array. This is a very common thing to do so optimizing could be worth it. Making it possible to compare a value to multiple other values in a single comparison would mean less code to write and more readable code imo

When comparing a value, you can put multiple values inside brackets [] and would return true if the comparison is true for all values. The second = in the if x == y would use AND, having ~ be the second character in the comparison would use OR

local otherValue = 3

-- Current solution
if (self.SomeValueName == 1 or self.SomeValueName == 2 or self.SomeValueName == otherValue) then

end

-- New solution
if (self.SomeValueName == [1, 2, otherValue]) then -- x == 1 and x == 2 and x == otherValue
	
end

if (self.SomeValueName ~= [1, 2, otherValue]) then -- x ~= 1 and x ~= 2 and x ~= otherValue

end

if (self.SomeValueName =~ [1, 2, otherValue]) then -- x == 1 or x == 2 or x == otherValue

end

if (self.SomeValueName ~~ [1, 2, otherValue]) then -- x ~= 1 or x ~= 2 or x ~= otherValue

end

if (self.SomeValueName <~ [1, 2, otherValue]) then -- x <= 1 or x <= 2 or x <= otherValue

end

if ([1, 2, otherValue] >= self.SomeValueName) then

end

if ([1, 2, otherValue] >= [3, 5, 6]) then

end
8 Likes

Support.

Not only would this save time programming, but it might also be more efficient for the engine itself (I might be wrong though. We’ll see).

Seems like a nice improvement to something people don’t really notice anything wrong with.

While it’s not that often I’ve needed more than one or in an if statement, I can definitely see this getting a lot of use, either from old code being rewritten for one reason or another, or new code being written with this in mind.

For anyone that is struggling with writing or far too much, a small suggestion to make your code a little cleaner (and potentially more efficient) would be writing a for loop that compares with a table of things to compare to, only running code if it meets at least one of the things in the table (you might need a break or two if you want to avoid code being run multiple times if it meets multiple comparisons)

By the way, if you want to suggest Luau features you don’t have to wait on Roblox to get things moving, you can submit an RFC as a pull request to the Luau repo.

1 Like