Assert() without an error message?

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

Can you not just use return if it doesn’t find the animator?

local animator = model:FindFirstChild("Animator")
if not animator then return end
--If it gets here, the animator exists, continue your code

assert() is very simple, it the value given is false or nil, it errors
For Example, if you do:

local Bool = false
assert(Bool) 

This will error, as the Boolean is false, you can just check if the value is false or nil with if not Varibale then

This would Basically be assert:

function thing(value)
   if not value then -- if false of nil
      return false -- assert returns an error
   else -- if not false or nil
     return true
   end
end

I don’t understand the real problem… am I missing something?

Is it really that draining to type something like this?

if animator then

end

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 believe that you won’t find a solution that is easier than one line of code per check.

Well yes, but this wouldn’t stop the thread running thing from continuing, which is what I want.

you would do this to do so:

if thing(value) then
-- rest of your code

or, a very simple way:

if Instance then
-- code
else return end

I would agree with @bonkybincer , it not tedious to add a single if statement

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. :cold_sweat:

it not tedious to add a single if statement

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

Personally, I do this for my assertion method:

--[[

	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
1 Like

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.

It amounts to hundreds of lines of code… the script will only get perpetually longer… almost every animation requires multiple conditional statements… :cold_sweat:

That’s why you use a custom assert; it spans only a single line.

Then write it on one line of code, like this:

if animator then [whatever you want here] end

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. :laughing:

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.

The only possible way to do this is to use the task library or coroutines, but that would only work for created threads.

Never mind, looks like you can do task.cancel(script). Doesn’t seem to throw a type error.

Try this, @zoox12345559

local function assert<a>(assertion: a)
	if not (assertion) then
		task.cancel(script)
	end
end

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