Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What does assert do I see it in Objcprogramming?

Asked by 5 years ago

I see it like this

t = {}



t1 = {}
 setmetatable(t, t1)
 assert(getmetatable(t) == t1)

I read the wiki and I cant understand what its trying to say

0
What is the c code that you're getting? royaltoe 5144 — 5y
0
thats c? EmbeddedHorror 299 — 5y
0
i've never bothered to learn metatables ._.' EmbeddedHorror 299 — 5y

1 answer

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

Let the topic on OOP and metatables down for a second as they don't need to be used to explain assert (xy problem here?).

assert() is a Lua function that simply checks if the first argument's value is not false or nil. If it is either then it will throw an error. If provided the second argument, of type string, it will use this as it's error message.

print(assert(1==2, "1 is not 2, silly.")) -- should fail

--console:
--> 12:24:38.803 - :2: 1 is not 2, silly.

The uses for this includes error debugging. It's not a requirement for oop practices.


In your case that's provided, it uses getmetatable() to compare it to t1. Since t1 is the metatable for t then the assert should pass.

local t = {}

local mt = {}

setmetatable(t, mt)

print(assert(getmetatable(t) == mt)) --true

--If not then the error would give: assertion failed!

References:

PiL on assert()

Lua Globals

Let me know if you need something explained or if I had missed anything.
Ad

Answer this question