[Wish] One line while loop (similar to if)

Discuss the future of the AutoHotkey language
HotKeyIt
Posts: 1605
Joined: 29 Sep 2013, 18:35
Contact:

Re: [Wish] One line while loop (similar to if)

13 Jun 2015, 01:25

I suggest to reserve return, break and continue only and do not allow (x(), break).
Only those 3 should be supported in if x, y(), return z, everything else must be an expression.
Guest_20151017

Re: [Wish] One line while loop (similar to if)

17 Oct 2015, 16:08

jethrow wrote:
lexikos wrote:I really doubt that it's about there being less typing, either way.

I agree. I'm gonna go out on a limb & say it's because of the restrictions on how you can write your syntax (not un-similar to complaints against python). I don't want to add another line to my code - I want the simple condition & action on the same line - especially if I'm in a One-Liner type of mood. This becomes extremely important when I'm trying to write beautiful, condensed code - or just trying to impress others with my sophisticated coding skills.

... of course I'm being sarcastic, yet truthful ...

Anyways, I'm gonna jump the gun & say why not take the perl approach and allow:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

action if conditon
; or
action while condition


That perl approach seems a bit backwards to me for putting the while and if toward the back, which isn't what happens in the non-one liners. I can see until being done like that though.
sirksel
Posts: 33
Joined: 12 Nov 2013, 23:48

Re: [Wish] One line while loop (similar to if)

16 Dec 2015, 22:29

lexikos wrote:Implementing the following is trivial:

Code: [Select all] [Download] GeSHi © Codebox Plus

while condition, statement
for var in expression, statement
for var1, var2 in expression, statement
expression, statement ; as in:
x(), return y
; even:
if x, y(), else, z()
It is less trivial to intuitively group the statements, as in if x, y(), return z grouping the last two in a block (or likewise when below an if/loop/etc.).

I may not be understanding, but I think what you've outlined above is useful (at least for me) just by itself, without any grouping functionality. I think, currently, all of these commands support a single unblocked statement immediately following the line (e.g., if you want to omit the braces), right? In that case, if I just want to use a single line with no braces I already can't have multiple statements on that line. I can have multiple assignments/expressions, but not multiple statements, correct? Even so, I use this no-braces option all the time for simple constructs. Isn't this proposal then just allowing that line to be moved up to the same line as the while, with all the same restrictions that would be there if this were a one-liner (unbraced) block?

For me, it would help a lot. I have lots of terse utility functions that are not that complicated to understand, and for these, compact is better. So, for example, ...

Code: [Select all] [Download] GeSHi © Codebox Plus

while count<=max,  execute(count++)
;is better (for me) than
while count<=max
execute(count++)
;the extra line doesn't really help things that much if the construct is simple.

If (i) it's just an optional thing for those who want/need it, (ii) it already mirrors the restrictions that are present in the one-liner (unbraced) form, and (iii) it's trivial to implement, I'd really appreciate it if you would include it when you have time. I, for one, would use it a great deal for simple functions. Thanks again!
lexikos
Posts: 5686
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [Wish] One line while loop (similar to if)

17 Dec 2015, 03:54

Being "useful" isn't enough. AutoHotkey v1 is already useful as it is. v2 is not about making the language more useful. A large part of it is about eliminating sources of confusion. Some of these sources of confusion are special rules within the language which were intended to make it more useful but end up creating inconsistency, like x := 1, y = 2 allowing = to assign an expression.

If if x, y(), return z was interpreted as
that would be a source of confusion. The grouping problem is specific to expression, statement, which would be one way of allowing if condition, expression, else, statement. Obviously, the others could be allowed without this.

Allowing expression, statement but not SomeCommand, % expression, statement could be another source of confusion. Allowing the latter - with restrictions like "must be after the last parameter of the command" - would create more complication (harder to understand, remember, implement...).

In C-like languages, the return in if (x) y(); return z; does not belong to the if, but in those languages, the semicolon is clearly a statement separator, never used to delimit parameters or sub-expressions. In AutoHotkey, if x (newline) y(), z() works just fine because y(), z() is a single expression with the comma ("multi-statement") operator. But if expression, statement was allowed and simply treated as two separate lines (no block/grouping), either y(), return z() and y(), z() would be inconsistent, or worse, if x, y(), z() would be interpreted as

Code: [Select all] [Download] GeSHi © Codebox Plus

if x
y() ; conditional upon x
z() ; unconditional

Allowing catch, var, statement would create another source of confusion, because someone is bound to write something like catch, return, where return is a variable intended as a statement.


In this case, the "usefulness" is subjective, and I think probably not in favour of the "non-programmers" who are ostensibly the main target of AutoHotkey.
User avatar
BGM
Posts: 391
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: [Wish] One line while loop (similar to if)

12 Nov 2017, 20:19

I always wanted the option to write loops on one line, too. It would help me organize my code better, and for me, at least, to read it more easily, especially for short loops like what fincs offered as an example above.
_3D_
Posts: 160
Joined: 29 Jan 2014, 14:40

Re: [Wish] One line while loop (similar to if)

02 Jan 2018, 04:19

In many sources We see next style:

Code: [Select all] [Download] GeSHi © Codebox Plus

if (expression) {
oneonlyline();
}
Same-line syntax prevent further editing of script and made source less readable.
In other case parsing become more complicated.

Code: [Select all] [Download] GeSHi © Codebox Plus

if expressinon1, expression2, action1, action2
Same-line but what will be the result?

Code: [Select all] [Download] GeSHi © Codebox Plus

if(expression1, expression2)
action1, action2 ;same-line 2 actions
OR

Code: [Select all] [Download] GeSHi © Codebox Plus

if(expressinon1, expression2, action1, action2)
"" ; the action
OR

Code: [Select all] [Download] GeSHi © Codebox Plus

if(expressinon1)
expression2, action1, action2 ;same-line 3 actions

Removing same-line in IF syntax separate expressions from actions by default.
AHKv2.0 use the future now.

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 1 guest