Distinguish BOOL from number

Discuss any general programming issues here
Post Reply
User avatar
Clyde
Posts: 180
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Distinguish BOOL from number

Post by Clyde » Fri Jul 19, 2019 11:03 am

Hey there,

for my little project where I port a lua script to Hollywood I need to explicitly distinguish between bool values (true|false) and numbers (1|0).

If I am using GetType() on boolean values I get #NUMBER which isn't of any help when I want to know whether this was the value "false" or "0".

So, is there a way to find out that the value was a boolean and not a number?
Currently using: Hollywood 7.1 with Windows IDE

SamuraiCrow
Posts: 222
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Distinguish BOOL from number

Post by SamuraiCrow » Fri Jul 19, 2019 4:56 pm

You'll have to do a type enumeration for every variable to keep track yourself.

PEB
Posts: 246
Joined: Sun Feb 21, 2010 1:28 am

Re: Distinguish BOOL from number

Post by PEB » Fri Jul 19, 2019 10:57 pm

I don't know if it would be too much trouble to rewrite your script to do it this way, but what I normally do if I want to show a negative result in a way that will not be confused with False or Nil is to return -1. (But that only works if there is no possibility of -1 being a normal/positive return.)

Another, and possibly better, way is to have your function return an extra result that can be checked separately. For example, if this is your function:
val=p_MyFunc()

change it to this instead:
val, bool=p_MyFunc()

Now you can check your val return as a number and your bool result separately.

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

Re: Distinguish BOOL from number

Post by airsoftsoftwair » Mon Jul 22, 2019 6:13 pm

In contrast to Lua, Hollywood doesn't have a specific boolean type but just numbers. So there is no way to distinguish a boolean from a number because in Hollywood stuff like "True" and "False" are just numbers that evaluate to 1 and 0, respectively.

By the way, this might be an interesting read for you. It also says the following about booleans:
Hollywood does not support the boolean object type. In Hollywood, the values True and False are simply special constants that will be mapped to the numerical values 1 and 0 respectively. There is no special object type for boolean values. This means that comparing 0 against False will be True in Hollywood, whereas in Lua it would be False because you would be comparing two different object types. Internally, Lua's boolean API is still supported by the VM and your plugin could use the respective functions from LuaBase but this is not recommended since Hollywood itself will never use Lua's boolean object type. It will always just use numbers. Not to mention that it is impossible to pass a real Lua boolean value to one of your plugin's functions because the parser will map all the True and False keywords to plain numbers.

Post Reply