Page 1 of 1

assert(nil)

PostPosted: Fri Sep 30, 2016 09:07
by SegFault22
I'm making a mod that creates functions to be used by other mods for stuff like registering many items/nodes/tools more easily than by writing out every single "minetest.register_*()" call in full (my mod automatically fills in many parameters by using properties stored in tables earlier). There may be some instance where some parameters get passed to a function, said parameters being of the correct type (string expected, got string; table expected, got table) but one or more are not valid, because they were not inserted to some special table which has to contain them along with associated properties. Later during loading, said table will be accessed to look-up properties relevant to specific entries, which would cause a crash if we try to access an index that doesn't exist in the table - this kind of crash is as graceful as, for example, falling down a staircase when you know it is there and are looking straight at it, but decided to assume that the first step was at the same height level as the floor at the top.

Is it more graceful to run "assert(nil)" as soon as the invalid parameter is encountered, or would that make my mod "malware"? I remember from over at the Mneincraft-Forge community, in the past GregTech would change the crafting recipes of other mods, and if any of said mods changed the recipes back, GregTech would intentionally cause a crash by some analogous method to "assert(nil)" (except in java of course), and people said that it's malware because it intentionally causes undesired operation of the system/machine when the user tries to use it.

Is there some better way to force the loading to be stopped, without my mod being called "malware" - or is assert(nil) acceptable for a serious mod?

Re: assert(nil)

PostPosted: Fri Sep 30, 2016 09:20
by rubenwardy
error("some readable error message") would be better

it's perfectly fine to throw an error if your functions are misused - that's validation

Re: assert(nil)

PostPosted: Fri Sep 30, 2016 15:07
by SegFault22
rubenwardy wrote:error("some readable error message") would be better

it's perfectly fine to throw an error if your functions are misused - that's validation

Okay, I'll use that wherever assert(<some important parameter>,<error message>) doesn't fit well. I thought error(<error message>) was just a neat way to handle errors without stopping the program (try to work around the error without stopping), but now that I think more about it, that doesn't make sense (if there's an error, the program must stop).

Thank you