AutoHotkey Community

It is currently May 26th, 2012, 9:28 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next

Should "While(...)" be a loop or a function?
A function: Preserves backward compatibility & avoids having a "forbidden" function name.
A loop, for consistency with the existing "if(...)" & for convenience of those in the habit of writing "while(...)"
You may select 1 option

View results
Author Message
 Post subject: while-loop
PostPosted: August 4th, 2009, 4:13 am 
Offline

Joined: August 2nd, 2009, 6:40 am
Posts: 215
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: while-loop
PostPosted: August 4th, 2009, 5:10 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 5:49 am 
Offline

Joined: August 2nd, 2009, 6:40 am
Posts: 215
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 7:45 am 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 8:03 am 
Offline

Joined: August 2nd, 2009, 6:40 am
Posts: 215
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 8:22 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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 August 4th, 2009, 8:49 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 8:48 am 
Offline

Joined: August 2nd, 2009, 6:40 am
Posts: 215
@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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 9:00 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 9:35 am 
Offline

Joined: August 2nd, 2009, 6:40 am
Posts: 215
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 10:12 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 1:48 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 4th, 2009, 7:28 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 5th, 2009, 1:15 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
Too much concern backwards can block progressing. I wonder how ended in choosing name like InStr then.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 11th, 2009, 12:28 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 11th, 2009, 12:52 pm 
hi chris, i support the change :)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group