I want to check if something exists/is true without using conditional statements. assert is pretty much exactly what I want, but I really don’t appreciate the error messages that it spews out. I’ve attempted to recreate assert while leaving out the errors to no avail.
What I’m doing:
I’m doing animations; I’m trying to find the animator of a model, and if it doesn’t find any then it cancels the animation function. I can use conditional statements, but they’re a bit draining and I’d like an easier method to use.
Currently, if there’s no animator found in a model, then the output will get FLOODED with error messages because of assert
I do actually want to return/halt the function, it’s just that… I’m being drained of my LIFE ENERGY by having to put hundreds of conditional statements all over my code (no matter if they’re 20 lines apart)
I am perpetually agonized by short conditional statements, though this does seem to be the physically “”"“easiest”""" solution, my soul can only take so much.
I agree, adding a single if statement is not tedious, but since I have to repeat this check every time that I have to play an animation, it wares on me
--[[
Throws an error in the console if the provided value is false or nil.
@param [any] assertion - The value to be asserted against.
@param [string] msg - The message to be errored.
@returns [any | nil] Returns the provided value if the assertion res-
olves to true, else nil.
--]]
function Package.assert<a, b>(assertion: a, msg: b): a?
if not (assertion) then
loggingManager.logMessage(msg, "assertion")
return nil
end
return assertion
end
Then, you could overwrite the function in the local scope by doing:
local function assert()
-- my function if you wish to us it
end
Conditional statements are the best way to tell the difference between nil and any other value of any type.
C’mon man, it’s 3 lines of code tops… unless every instance and value you’re programming is frequently nil, I still don’t understand how using conditional statements is even a problem.
You should still be following the D.R.Y. principle, adding single line conditional statements looks god awful anyways & does not follow the Luau style guide.
(to re-iterate:)
I don’t want to give an error message, I want to cancel the thread running the assert if assert fails (returns false). I do not want to use conditional statements for every check. If this is not possible then tell me
Personally, I would never write a conditional statement or function on one line of code… I need my code to be readable for when I come back to it 4 months later.
Because @zoox12345559 doesn’t want lines and lines of conditional statements, the only way to reduce the number of lines is by writing each one on a single line, unless they want to follow the D.R.Y. principle like you mentioned, and write a function for it.
I want to reduce the strain on my mind and body by avoiding conditional statements. Sure, they may be readable in the moment, but if I know what assert does then it’s far quicker for me to process it rather than try to muffle out the noise that a conditional statement makes