Page 1 of 1
Treat missing table item the same as a NIL-variable
Posted: Fri May 28, 2021 11:00 am
by mrupp
In my app, I'm using a lot of tables, not as arrays but as objects with attributes, and there are lots of places where I have to check for an attribute to be set or not. Testing the attribute directly by accessing it throws an error, though, while this is not the case when accessing some other variable that has no value (Nil).
Example:
Code: Select all
Local testVar
testVar = Nil
If testVar ; no problem here
DebugPrint(testVar)
Else
DebugPrint(testVar) ; ouputs "Nil"
EndIf
Local table = {}
table.test = Nil
If table.test ; throws the following error: Table field "test" was not initialized!
DebugPrint(table.test)
Else
DebugPrint(table.test)
EndIf
Of course I can work around this by using an additional
HaveItem():
Code: Select all
If HaveItem(table, "test") And table.test
DebugPrint(table.test)
EndIf
Now, my suggestion is not to throw an error when trying to access a nonexistent table attribute but to return Nil instead, which would make it easier to check an attribute to be present in an If-statement without having to use an additional
HaveItem().
Re: Treat missing table item the same as a NIL-variable
Posted: Fri May 28, 2021 10:58 pm
by p-OS
mrupp wrote: ↑Fri May 28, 2021 11:00 am
Now, my suggestion is not to throw an error when trying to access a nonexistent table attribute but to return Nil instead, which would make it easier to check an attribute to be present in an If-statement without having to use an additional
HaveItem().
You could assign a metatable to your tabel that overloads/implements __index metamethod.
https://www.hollywood-mal.com/docs/html ... OprMM.html
This metamethod is called whenever you try to read from a table index that does not exist. This metamethod could be used to create a default value for all uninitialized table fields.
Re: Treat missing table item the same as a NIL-variable
Posted: Sat May 29, 2021 11:58 am
by jPV
You can use
RawGet(), it won't fail if a key doesn't exist.
Code: Select all
If RawGet(table, "test")
; these should do the same...
DebugPrint(table.test)
DebugPrint(RawGet(table, "test"))
EndIf
Re: Treat missing table item the same as a NIL-variable
Posted: Sun May 30, 2021 3:39 am
by SamuraiCrow
jPV wrote: ↑Sat May 29, 2021 11:58 am
You can use
RawGet(), it won't fail if a key doesn't exist.
Code: Select all
If RawGet(table, "test")
; these should do the same...
DebugPrint(table.test)
DebugPrint(RawGet(table, "test"))
Else
DebugPrint("Not found.")
EndIf
Fixed your example, jPV.
@op
RawGet() is the function you're looking for though, it returns type Nil in the event of a failed search.
Re: Treat missing table item the same as a NIL-variable
Posted: Sun May 30, 2021 5:44 pm
by airsoftsoftwair
mrupp wrote: ↑Fri May 28, 2021 11:00 am
Now, my suggestion is not to throw an error when trying to access a nonexistent table attribute but to return Nil instead, which would make it easier to check an attribute to be present in an If-statement without having to use an additional
HaveItem().
Sorry, won't come. Lots of internals depend on the behaviour as it is. Changing this has the potential to make all hell break loose.