Actually ran into this… I thought it did work, but :tobool is just extremely conservative in what it converts. I did think it dealt with -1 as “false” – that isn’t true, it’s true. Very confusing difficult bug in one of my script… Anyway, why I write here so it’s a little more clear how this works to me – my help to others.
I think this is right about "bool type:
:tobool only takes a [:typeof $x]=“num”, where only/exactly a num type of 0 is false, all other num are true. Thus, negative numbers, including -1, are also true.
The tri-state return (e.g. “nil”), especially for “-1”, is the biggest issue here IMO since it means :tobool is “unsafe”. :while (true) :for :toip it also returns nil, instead of say 0.0.0.0, for an invalid IP value provided to :toip…
I think this is the simplest script to show what’s happening:
booltest=true using -1
booltest=false using 0
booltest=true using 1
booltest= using "1" and returns typeof nil
booltest=true using [tonum "1"] and returns typeof bool
from the following:
:global booltest [:tobool -1]
:put "booltest=$booltest using -1"
:global booltest [:tobool 0]
:put "booltest=$booltest using 0"
:global booltest [:tobool 1]
:put "booltest=$booltest using 1"
:global booltest [:tobool "1"]
:put "booltest=$booltest using \"1\" and returns typeof $([:typeof $booltest])"
:global booltest [:tobool [:tonum "1"]]
:put "booltest=$booltest using [tonum \"1\"] and returns typeof $([:typeof $booltest])"
My vote is a more liberal approach be more useful, since ROS uses many “synthetic bools” like “yes”/“no” in particular today:
- if num, =<0 for false (thus >0 for true) - like the conditionals :while or :if
- “yes”/“no”, as used in “disabled=yes” etc
- if string, “” means false, any text but “no” above is true
- if nothing, false
- if nil, nothing false
- if :ip, :ip6, others, true if “something” valid.
I’m sure others heavily using scripts have functions mask some of the weird type artifacts – i.e. looking at [:nothing], but see nil. Now I did learn [:tobool “1”] is one easy way to assign nil…