if var is [not] type

Get help with using AutoHotkey and its commands and hotkeys
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

if var is [not] type

11 Dec 2015, 20:53

Not sure if this quite qualifies as a bug, but the behavior is odd...

If "var" is NOT a variable, but an expression:

Code: Select all

if var is number
Has unpredictable(ish) results.

Code: Select all

var := 1
q := [ "Fish", "Dog", "Cow" ]
if var is number
    msgbox, Yup, it's a number
if var + 1 is number
    msgbox, Well, no, it's not actually a number.
if "Fish" + 1 is number
    msgbox, Neither is this.
if q[2] is number
    msgbox, Nor is this.
I would expect either
1) an error report
2) FALSE (an expression is NOT a number)

It'd be kinda nice if
3) You could use an expression.
4) The documentation were more explicit. Ya, I read it. Ya it says "variable". But, one more sentence to say it MUST be a variable and not a function, array, expression, etc. wouldn't hurt. :-)
lexikos
Posts: 7085
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: if var is [not] type

11 Dec 2015, 21:56

No, it is not a bug.

The documentation is explicit about the syntax for if var is [not] type:

Code: Select all

if var is type
if var is not type
... and var is defined as "The variable name."

var + 1 is not a variable name. It is evidently an expression.

if var + 1 is number is not "if var is type". It is "if expression", equivalent to this:

Code: Select all

if (var + 1 . is . number)
Both is and number are variables, because:
Note: The operators "between", "is", "in", and "contains" are not supported in expressions.
Peabianjay wrote:msgbox, Well, no, it's not actually a number.
As a matter of fact, the expression var + 1 is number is usually only true if var contains a number, since is and number are usually empty and var + 1 would produce a non-empty, non-zero value only if var is a number. However, it will be false if var is -1. You can use var+1 != "" in an expression to detect if var contains a number.
Peabianjay wrote:Not sure if this quite qualifies as a bug
In general, if there is doubt, the appropriate place to post is Ask for Help. I have moved the topic.
Peabianjay
Posts: 47
Joined: 07 Nov 2015, 22:50

Re: if var is [not] type

13 Dec 2015, 10:29

Thanks for the explanation.
if (var + 1 . is . number)
That's exactly where I got confused. Between the unquoted text treated as text, and undeclared variables still being treated as variables, I get befuddled.
In general, if there is doubt, the appropriate place to post is Ask for Help. I have moved the topic.
Oh ya, lots & lots of doubts. Heh. Noted for the future. (Still trying to get used to the way AHK interprets stuff.)

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], boiler and 32 guests