The is operator does not count 0 as an integer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

The is operator does not count 0 as an integer

09 Nov 2019, 21:18

If you write

Code: Select all

MsgBox, % StrLen( "" ) is integer
or just

Code: Select all

MsgBox, % 0 is integer
, AHK will output false. In my opinion, this is an error. For other programming languages 0 is considered an integer number.
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: The is operator does not count 0 as an integer

09 Nov 2019, 21:36

It's not a bug. Note this in the IfIs doc:
Note: The operators "between", "is", "in", and "contains" are not supported in expressions.
Your MsgBox, % makes it an expression.

Try this:

Code: Select all

If 0 is integer
  MsgBox 0 is integer
Else
  MsgBox 0 is not integer
Regards, Joe
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: The is operator does not count 0 as an integer  Topic is solved

09 Nov 2019, 22:53

as Joe said, its not a bug.

in AHK v1, is is NOT an operator. the construct if varname is type is the full command, and the only way it can be used

but even this is not really correct:
JoeWinograd wrote:
09 Nov 2019, 21:36
Try this:

Code: Select all

If 0 is integer
  MsgBox 0 is integer
Else
  MsgBox 0 is not integer
the if-construct MUST use a variable name. it cannot be a value. the only reason the above works is because the variable named "0" is undefined. see here:

Code: Select all

0 := "hello"
If 0 is integer
  MsgBox 0 is integer
Else
  MsgBox 0 is not integer

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: The is operator does not count 0 as an integer

09 Nov 2019, 23:23

0 is a variable which holds the number of command line parameters, presumably 0 in these cases.

Edit, @JoeWinograd, :thumbup: Cheers.
Last edited by Helgef on 10 Nov 2019, 02:12, edited 1 time in total.
User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: The is operator does not count 0 as an integer

10 Nov 2019, 02:08

Thanks guest3456 and Helgef...both are very interesting comments! Clearly, the far better test for madsounds is:

Code: Select all

var:="0"
If var is integer
  MsgBox var=%var% and is integer
Else
  MsgBox var=%var% and is not integer

var:="x"
If var is integer
  MsgBox var=%var% and is integer
Else
  MsgBox var=%var% and is not integer
Regards, Joe
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

Re: The is operator does not count 0 as an integer

10 Nov 2019, 15:42

Oh I see now :o It turns out that it is the "if ... is integer", without brackets. Because brackets (or anything else like MsgBox) form an expression.

And this example will work:

Code: Select all

IsInteger( var )
{
	if var is integer
	{
		return true
	}
	else
	{
		return false
	}
}

MsgBox, % IsInteger( 0 )
But that one will not:

Code: Select all

IsInteger( var )
{
	if( var is integer )
	{
		return true
	}
	else
	{
		return false
	}
}

MsgBox, % IsInteger( 0 )
Thank you guys so much!
guest3456
Posts: 3469
Joined: 09 Oct 2013, 10:31

Re: The is operator does not count 0 as an integer

10 Nov 2019, 20:57

madsounds wrote:
10 Nov 2019, 15:42
Oh I see now :o It turns out that it is the "if ... is integer", without brackets. Because brackets (or anything else like MsgBox) form an expression.
correct.

AHK v1 is very quirky. there are many instances of "if" statements which are not expressions, but rather should be thought of as if-commands with specific syntax:

if var <=> value
https://www.autohotkey.com/docs/commands/IfEqual.htm
typically this is called "traditional-if" rather than "expression-if" which would require parenthesis

if var is type
https://www.autohotkey.com/docs/commands/IfIs.htm

if var in/contains list
https://www.autohotkey.com/docs/commands/IfIn.htm

if var between lower and upper
https://www.autohotkey.com/docs/commands/IfBetween.htm


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Xtra and 248 guests