Logan West
Logan West

Reputation: 45

Lua Assert Triggers Function Even with True Evaluation

Using assert, how is it when I use a function as the output it always performs the function no matter if the expression is true of false? Example:

local function printError()
    print("Must be 4!")
end

Q1 = 4

assert(Q1 == 4,printError())

--Output
--Q1 = 1 >> Must be 4!
--Q1 = 4 >> Must be 4!

The function is called no matter if it's true or false.

But if I use a simple string as the output of the assert, then it asserts correctly:

Q1 = 4

assert(Q4 == 4,"Must be 4!")

--Output
--Q1 = 1 >> Must be 4!
--Q1 = 4 >>

Upvotes: 1

Views: 219

Answers (1)

Marc Balmer
Marc Balmer

Reputation: 1830

assert expects a string as it's second argument. If you pass function, this function is executed before the actuall assert is executed.

Upvotes: 1

Related Questions