Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

IsFunc("f" ""+1) returns true, but IsFunc("f" x+1) returns false even if x == ""



  • Please log in to reply
11 replies to this topic
GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
x := ""
 
MsgBox, % IsFunc("f" (x+1))
MsgBox, % IsFunc("f" (""+1))
 
f1(){
}

 

Code to reproduce the issue



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
x := ""
MsgBox, % "|" . (x+1) . "|"  ; calculated   (results in "" because x is empty)
MsgBox, % "|" . (""+1) . "|" ; concatenated (because the first value is a literal string)

Prefer ahkscript.org for the time being.


GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

O_o I could've sworn that x+1 returned 1. I suppose that the IsFunc() isn't the problem, then. So, why doesn't x+1 == 1?



VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
I believe some time ago, Lexikos tweaked the way AHK interprets operators following a quoted literal string. In the case of ( "#" &var ), the assumption is that the user wants to concatenate 'var's address to a literal string. Since + and - are also unary operators, I guess the same tweak applies.

I.e.(""+1) is treated as ("".(+1)).

GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

But why does blankvar+1 == blank, instead of concat or addition treating blankvar as 0?



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

What would you expect as the result of adding 1 to nothing?

 

BTW: It wasn't Lexikos.


Prefer ahkscript.org for the time being.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer

BTW: It wasn't Lexikos.

 

The behaviour when using blank values with math operators (blankvar+1) is separate to what VxE described (""+1).  VxE is correct.

 

"" &var was the main concern for that change, and "" +1 mostly for consistency.  Before "" +1 was treated as concatenation, it produced an empty string; i.e. because it is an invalid operation, as was "" & n (bitwise-and).



GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
a++
b+=1
c:=c+1
MsgBox, % a ", " b ", " c ; 1, 1, Blank

Just me, I thought adding 1 to nothing was 1



just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011

Lexikos, you're right, I remember now. I 'wasn't amused' about that change.
 
GeekDude, sorry, my question wasn't detailed enough. This behaviour is related only to expressions:
 

Except where noted below, any blank value (empty string) involved in a math operation is not assumed to be zero. Instead, it is treated as an error, which causes that part of the expression to evaluate to an empty string. For example, if the variable X is blank, the expression X+1 yields a blank value rather than 1.


Prefer ahkscript.org for the time being.


iPhilip
  • Members
  • 138 posts
  • Last active: Sep 12 2015 06:27 PM
  • Joined: 03 Jun 2010
a++
b+=1
c:=c+1
MsgBox, % a ", " b ", " c ; 1, 1, Blank

Just me, I thought adding 1 to nothing was 1

 

 

I get the same result. If I initialize all the variables ahead of time, e.g.

a := b := c := 0

I get the expected result.


Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)


GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009

If I initialize all the variables ahead of time

 

That's the point. If I don't have to initialize in most cases, why initialize in some cases?



iPhilip
  • Members
  • 138 posts
  • Last active: Sep 12 2015 06:27 PM
  • Joined: 03 Jun 2010

I agree. It doesn't make sense.


Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)