Treat missing table item the same as a NIL-variable

Feature requests for future versions of Hollywood can be voiced here
Post Reply
User avatar
mrupp
Posts: 83
Joined: Sun Jan 31, 2021 7:44 pm
Location: Switzerland
Contact:

Treat missing table item the same as a NIL-variable

Post 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().

p-OS
Posts: 144
Joined: Mon Nov 01, 2010 11:56 pm

Re: Treat missing table item the same as a NIL-variable

Post 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.

User avatar
jPV
Posts: 394
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Treat missing table item the same as a NIL-variable

Post 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

SamuraiCrow
Posts: 407
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Treat missing table item the same as a NIL-variable

Post 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.
I'm on registered MorphOS using FlowStudio.

User avatar
airsoftsoftwair
Posts: 4668
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Treat missing table item the same as a NIL-variable

Post 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.

Post Reply