AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

while-loop
Goto page 1, 2, 3  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Wish List
View previous topic :: View next topic  

Should "While(...)" be a loop or a function?
A function: Preserves backward compatibility & avoids having a "forbidden" function name.
26%
 26%  [ 5 ]
A loop, for consistency with the existing "if(...)" & for convenience of those in the habit of writing "while(...)"
73%
 73%  [ 14 ]
Total Votes : 19

Author Message
Crash&Burn



Joined: 02 Aug 2009
Posts: 210

PostPosted: Tue Aug 04, 2009 3:13 am    Post subject: while-loop Reply with quote

BUG? Or by design?

You cannot do (inside a function):
Quote:
while( A_index < 5 )

While needs a space before the open bracket:
Quote:
while ( A_index < 5 )


Note from Chris: I added the poll on 9/27/2009.
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Aug 04, 2009 4:10 am    Post subject: Re: while-loop Reply with quote

Crash&Burn wrote:
BUG? Or by design?

You cannot do (inside a function):
Quote:
while( A_index < 5 )

You cannot do while( A_index < 5 ) anywhere unless you have a function named while()
While needs a space before the open bracket:
Quote:
while ( A_index < 5 )
All commands require a delimiter (space/tab/comma) to distinguish them from functions. Functions may not have a delimiter between the function name and the "(".
And this should not be in bug reports! You (apparently) are a new ahk user and should not consider all problems to be bugs, but a lack of experince/understanding. Consider posting problems in ask for help, then if a bug is confirmed, post it here.
Back to top
Crash&Burn



Joined: 02 Aug 2009
Posts: 210

PostPosted: Tue Aug 04, 2009 4:49 am    Post subject: Reply with quote

if() doesn't

Yes very new. A_ParamCount - to retrieve the number of PASSED parameters
And barely/if any "commands" require spaces.
Code:
foo=foobar
if(regExMatch(foo,"foo(bar)",bar))
{
   MsgBox, "bar: %bar1%"
}
return
or
Code:
foo=foobar
Foo( foo )

Foo( foo )
{
   pos:=regExMatch(foo,"foo(bar)",bar)

   MsgBox, "pos:%pos%"
}
return
or
Code:
#ifWinActive, ahk_class OpWindow
{   ~LButton::
   {
      if GetKeyState("Shift") == 1
      {
         Send, ^{LButton}
      }
      else
      if GetKeyState("Ctrl") == 1
      {
         Send, {Ctrl Up}+{LButton}{Ctrl DownTemp}
      }
   }
   return
}
return

In fact, in over a year, while() is the first internal command that has choked on a lack of a space.

And Bite me.
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Aug 04, 2009 6:45 am    Post subject: Reply with quote

Crash&Burn wrote:
if() doesn't
And barely/if any "commands" require spaces.

If you are going to rely on undocumented behavior, expect to have lots of problems. While the documentation does not specifically state that keywords must be separated, I challenge you to find any example or case in the help file that suggests that delimiters are not needed.
Crash&Burn wrote:
And Bite me.
Ok, I'll sink to your level of maturity.
Bite yourself
Back to top
Crash&Burn



Joined: 02 Aug 2009
Posts: 210

PostPosted: Tue Aug 04, 2009 7:03 am    Post subject: Reply with quote

Yes, I have read the helpfile. Have You?

Almost every function in there, is shown without "whitespace" between functionname and brackets.

Perhaps you might find [Contents]
+ Functions
...+ Math

or, GetKeyState, listed under + Keyboard Control
and usage listed as:
Quote:
KeyIsDown := GetKeyState("KeyName" [, "Mode"])

or perhaps, DllCall, listed under + Misc Commands:
Quote:
Result := DllCall("[DllFile\]Function" [, Type1, Arg1, Type2, Arg2, "Cdecl ReturnType"])

or perhaps, all of the functions listed under + String Management, that accept brackets in their usage.

So again, I ask, is the requirement of whitespace in "while (x)", a BUG or by Design?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Tue Aug 04, 2009 7:22 am    Post subject: Reply with quote

While is not a function. Notice that it can't be positioned arbitrarily in a statement. And AHK's functions can't have space(s) between name and opening parenthesis, due to the implicit concatenation in AHK. Anyway, I noticed this myself a while ago. It can be confusing to some users as the following works on the contrary.
Code:
While!(A_Index=5)
   MsgBox, %A_Index%

And, I think there is a side-effect of this, which is not good IMO.
Code:
While(x)
{
   MsgBox, %x%
}
While(3)


Last edited by Sean on Tue Aug 04, 2009 7:49 am; edited 1 time in total
Back to top
View user's profile Send private message
Crash&Burn



Joined: 02 Aug 2009
Posts: 210

PostPosted: Tue Aug 04, 2009 7:48 am    Post subject: Reply with quote

@Sean, yes and neither is "if()"

While is part of the "flow of control", "While ()" and "if() || if ()" appear to be the only Control Commands/Statements that accept brackets.

There has been discussion of For() being added. And a furthering of most commands being functionally() callable.

I can certainly add a space after a While statement. It just seems more than odd that it is required to.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Tue Aug 04, 2009 8:00 am    Post subject: Reply with quote

Crash&Burn wrote:
I can certainly add a space after a While statement. It just seems more than odd that it is required to.
I was neutral on this, but now I agree with you. See my last example.
Back to top
View user's profile Send private message
Crash&Burn



Joined: 02 Aug 2009
Posts: 210

PostPosted: Tue Aug 04, 2009 8:35 am    Post subject: Reply with quote

I was leery of trying that, thought it might spam messageBox'es out of control heh. Instead x gets assigned 3...
whereas, this:
Code:
Foo( foo )
Foo( foo )
{
   While(x)
   {
      MsgBox, %x%
   }
   While(3)
}
return
Quote:
Error at line 4.
Line Text: While(x)
Error: Functions cannot contain functions.
Code:
Foo( foo )
{
   While (x)
   {
      MsgBox, %x%
   }
   While(3)
}
Quote:
Error: Call to nonexistant function.
Specifically: While(3)
Code:
Foo( foo )
{
   While (x)
   {
      MsgBox, %x%
   }
   While (3)
}
Quote:
Error: Inappropriate line beneath IF or LOOP.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Tue Aug 04, 2009 9:12 am    Post subject: Reply with quote

Yes, the problem is that an user may create inadvertently a function instead a loop. I suppose that it's a consequence of following the syntax of Loop, not that of If. Notice that you can also use like
Code:
While, A_Index<5
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7295
Location: Australia

PostPosted: Tue Aug 04, 2009 12:48 pm    Post subject: Reply with quote

It is by design.
Crash&Burn wrote:
if() doesn't
'If' is not a command per se. It is used in syntax very distinct from commands; keywords are used to both delimit the arguments and determine what action/command it actually is:
Code:
If var
If var between low and high
If var in matchlist
If var contains matchlist
If var is type
If var = value
If var != value
If var > value
If var < value
etc.
From a "purely implementation" perspective, IF() is only allowed because an exception is specifically made for it when determining whether the line begins with a function call or definition:
Code:
   if (   !(action_end && *action_end == '(' && action_end != aBuf
      && (action_end - aBuf != 2 || strnicmp(aBuf, "IF", 2)))
      || action_end[1] == ':'   )
      return false;
Supporting While() seems to be a simple matter of adding an exception for it:
Code:
   if (   !(action_end && *action_end == '(' && action_end != aBuf
      && (action_end - aBuf != 2 || strnicmp(aBuf, "IF", 2))
      && (action_end - aBuf != 5 || strnicmp(aBuf, "WHILE", 5)))
      || action_end[1] == ':'   )
      return false;
Crash&Burn wrote:
While is part of the "flow of control", "While ()" and "if() || if ()" appear to be the only Control Commands/Statements that accept brackets.
return (%MyVar%) is one example given in the help file.
Loop (*) is a valid file loop, matching any filenames beginning with ( and ending with ).
Any argument which accepts text or an expression can legitimately begin with ( and end with ).


To clarify my point of view: it is by design, but designs can and sometimes should be changed.

Btw, none of the examples in the help file surround a While's expression with parentheses.
Back to top
View user's profile Send private message Visit poster's website
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Tue Aug 04, 2009 6:28 pm    Post subject: Reply with quote

Lexikos wrote:
It is by design.
Right; I thought it was for backwards compatibility: prior scripts may have used While() as a function, and Chris didn't want to break them.
Back to top
View user's profile Send private message Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Wed Aug 05, 2009 12:15 am    Post subject: Reply with quote

Too much concern backwards can block progressing. I wonder how ended in choosing name like InStr then.
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Tue Aug 11, 2009 11:28 am    Post subject: Reply with quote

Changing "While()" to be recognized as a loop rather than a function would not only break any existing script that uses "while" as a function name, it would break it in a way that is likely to cause infinite loops.

On the other hand, using "while" as a function seems extremely rare. Hopefully anyone who does so will read the changelog.

So I agreed changing this seems justified, especially since many who know other languages are in the habit of writing if() and while().

So unless there are any objections, it will be changed in the next release.

Thanks.
Back to top
View user's profile Send private message Send e-mail
Guest






PostPosted: Tue Aug 11, 2009 11:52 am    Post subject: Reply with quote

hi chris, i support the change Smile
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Wish List All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group