| 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% |
[ 5 ] |
| A loop, for consistency with the existing "if(...)" & for convenience of those in the habit of writing "while(...)" |
|
73% |
[ 14 ] |
|
| Total Votes : 19 |
|
| Author |
Message |
Crash&Burn
Joined: 02 Aug 2009 Posts: 210
|
Posted: Tue Aug 04, 2009 3:13 am Post subject: while-loop |
|
|
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 |
|
 |
Guest
|
Posted: Tue Aug 04, 2009 4:10 am Post subject: Re: while-loop |
|
|
| 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
|
Posted: Tue Aug 04, 2009 4:49 am Post subject: |
|
|
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 |
|
 |
Guest
|
Posted: Tue Aug 04, 2009 6:45 am Post subject: |
|
|
| 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
|
Posted: Tue Aug 04, 2009 7:03 am Post subject: |
|
|
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 |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Tue Aug 04, 2009 7:22 am Post subject: |
|
|
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 |
|
 |
Crash&Burn
Joined: 02 Aug 2009 Posts: 210
|
Posted: Tue Aug 04, 2009 7:48 am Post subject: |
|
|
@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 |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Tue Aug 04, 2009 8:00 am Post subject: |
|
|
| 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 |
|
 |
Crash&Burn
Joined: 02 Aug 2009 Posts: 210
|
Posted: Tue Aug 04, 2009 8:35 am Post subject: |
|
|
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 |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Tue Aug 04, 2009 9:12 am Post subject: |
|
|
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
|
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Tue Aug 04, 2009 12:48 pm Post subject: |
|
|
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 |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Tue Aug 04, 2009 6:28 pm Post subject: |
|
|
| 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 |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Wed Aug 05, 2009 12:15 am Post subject: |
|
|
| Too much concern backwards can block progressing. I wonder how ended in choosing name like InStr then. |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10716
|
Posted: Tue Aug 11, 2009 11:28 am Post subject: |
|
|
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 |
|
 |
Guest
|
Posted: Tue Aug 11, 2009 11:52 am Post subject: |
|
|
hi chris, i support the change  |
|
| Back to top |
|
 |
|