consider the function
function void GUItest( bool b1 "Boolean 1", bool b2 "Boolean 2")
printf "Type b1: %s, value: %g\n", typestr(b1),b1
printf "Type b2: %s, value: %g\n", typestr(b2),b2
return
end function
If put into a package I get the GUI as attached.
If I press "Apply", I get result 1:
Type b1: null, value: 0
Type b2: null, value: 0
If I check the first check box I get result 2
Type b1: null, value: 0
Type b2: scalar, value: 1
If I call the function GUItest() in a script, I had expected result 1 (in analogy to the GUI outcome and also to the case that b1 and b2 are defined defined as scalar b1[NAN] scalar b2[NAN]), but I get
? GUItest()
GUItest: not enough arguments
Command has insufficient arguments
Error executing script: halting
If I call GUItest(0,0) I get result 1.
If I call GUItest(,1) I don't get result 2 but rather
? GUItest(,1)
GUItest: argument 1 is of the wrong type (is null, should be bool)
Data types not conformable for operation
Error executing script: halting
I find this somewhat difficult to understand. My preference would be to handle Booleans just like scalars (as is the case when Booleans are checked, but not if unchecked). For my purposes the option [NAN] would be handy as well. Yet I am a lay person regarding programming. You may have another idea or some good reasons to keep things as they are. I just wanted to bring this to your attention.
Ekkehart
Hi Ekkehart, I haven't read your entire post yet (sorry), but in the beginning I think there's a misunderstanding: the function typestr() is meant to get as input the result of typeof(). Instead you put the value b1 directly in that, so if b1 is zero, then you're asking typestr() effectively: "Tell me, what's the object type associated with code 0?" - and that's nothing really, so "null".
You'd need to do typestr(typeof(b1)). But because that's a bit clunky, recently (in the latest release 2023b) the function typename() was introduced, which should do what you want there.
Boolean arguments have only two acceptable values: 0 or 1 (or the equivalent FALSE or TRUE). If you want a boolean argument to be optional in the scripting context you need to give it a default value, as in
function void foo (bool b[0])If for some reason you can't settle on a default value in advance, you can change the parameter into a three-way switch as in
function void foo (int b3[1:3:1] {"auto", "one_thing", "other_thing"}Then you can decide at run time whether to do one_thing or other_thing if the caller omits the argument and you get the default of 1.
OK, so there's no bug, just some misunderstandings, so I'm closing this.
(The only thing that sounds weird is your report that if you check the first box you get result 2, i.e. value 1 for b2. But I'm assuming that that's just swapping of what really happened. Otherwise please report back.)
For the record, here's a corrected version of Ekkehart's test function:
Thanks! This works as I expected, both in the GUI and in scripting. Sorry for my mistake!