Need more braces in loop blocks in v2

Report problems with documented functionality
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Need more braces in loop blocks in v2

Post by Relayer » 28 Feb 2023, 15:43

I got all tied up converting a library file to v2. Turns out that the problem appears to be that more braces are needed in multilevel loop blocks that weren't needed in v1. Perhaps this is intended?

Relayer

Code: Select all

#SingleInstance Force
#Requires AutoHotkey v2.0


function1([3, 2, 1], "moe", "jack") ;works as expected
function1("manny", "moe", "jack")   ;doesn't work in v2
function1("manny", "moe")           ;doesn't work in v2

function2("tom", "dick", "harry")   ;works as expected
function2("tom", "dick")            ;works as expected

function1(a, b, c := "")
{
	if (isObject(a))
		for k, v in a
			Msgbox("k: " . k . "`nv: " . v)
	else if (c = "")
		Msgbox("boo")
	else
		Msgbox(b)
}

function2(a, b, c := "")
{
	if (isObject(a))
	{
		for k, v in a
			Msgbox("k: " . k . "`nv: " . v)
	}
	else if (c = "")
		Msgbox("boo")
	else
		Msgbox(b)
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Need more braces in loop blocks in v2

Post by swagfag » 28 Feb 2023, 16:46

its because of a stupid pythonesque feature that was implemented in v2: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

its parsed as:

Code: Select all

if (isObject(a))
	for k, v in a
		Msgbox("k: " . k . "`nv: " . v)
	else ; else attached to for-loop, triggered when no loop iterations were made
		if (c = "")
			Msgbox("boo")
		else
			Msgbox(b)
so yes, its "intended"

joefiesta
Posts: 494
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Need more braces in loop blocks in v2

Post by joefiesta » 01 Mar 2023, 11:24

all I can say is WOW How screwed up is this? No wonder I would never want to convert my 100000 lines of ahk code to version 2.

User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Need more braces in loop blocks in v2

Post by Relayer » 01 Mar 2023, 12:20

As I said in my opening comments, I was converting an existing script and could not figure out why it wasn't working. I would never have thought an 'else' statement would pair with a 'for' loop. Python has a lot going for it but this and the reliance on indented source code are not two of them.

Relayer

ahkrpa
Posts: 17
Joined: 16 Apr 2019, 17:34

Re: Need more braces in loop blocks in v2

Post by ahkrpa » 01 Mar 2023, 18:19

Seems like GoSub > Label functionality could have been retained. Imagine most of 'breakage' in script construction would have been avoided.
Last edited by ahkrpa on 21 Mar 2023, 10:50, edited 1 time in total.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Need more braces in loop blocks in v2

Post by swagfag » 01 Mar 2023, 18:27

aint nobody talking bout no labels, not sure why u are

guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Need more braces in loop blocks in v2

Post by guest3456 » 02 Mar 2023, 21:41

Relayer wrote:
01 Mar 2023, 12:20
Python has a lot going for it but this and the reliance on indented source code are not two of them.
if AHK relied on indentation like python does, then your function1() would work as you intended it, with the else being attached to the correct if block

i struggle to see any downsides of forcing indentation


User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Need more braces in loop blocks in v2

Post by Relayer » 03 Mar 2023, 08:38

I just mentioned indentation as a personal preference and not to take the conversation off topic. The topic is that v2 assigns 'else' statements differently than v1 did and this may cause, and did cause in my case, a lot of wasted time debugging as my script failed silently. I hope to inform others so they don't make the same mistake.

Relayer

cat dog fox war
Posts: 38
Joined: 15 Mar 2023, 10:18

Re: Need more braces in loop blocks in v2

Post by cat dog fox war » 17 Mar 2023, 20:53

I liked this feature when I was writing python, but it would be better if ahk-v2 was designed with keywords like esle_for or elseFor

ahkrpa
Posts: 17
Joined: 16 Apr 2019, 17:34

Re: Need more braces in loop blocks in v2

Post by ahkrpa » 21 Mar 2023, 11:21

swagfag wrote:
01 Mar 2023, 18:27
aint nobody talking bout no labels, not sure why u are
@swagfag

fair point as i failed to provide context.

was agreeing with @joefiesta lamenting that "I would never want to convert my 100000 lines of ahk code to version 2".

i have 1,677 fully formed AutoHotkey v1.1 scripts (just counted) with x1,000s of lines in many of them; they intersperse functions, classes, and yes, labels.

IMHO if just Gosub > Labels logic/flow support had been carried forward i would have stayed with AutoHotkey and converted to v2.0 as all the other changes are tolerable.

joefiesta
Posts: 494
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Need more braces in loop blocks in v2

Post by joefiesta » 26 Mar 2023, 10:56

Converting to V2 - further thought

I would convert to V2 if for only one thing. A DEFINITIVE listing of EVERY syntax change that would be necessary, including V1 syntax and V2 syntax. This would mean I could--and I probably would--write an edit macro to automate the zillions of changes. The appeal of the improved syntax is enticing, but it is overwhelmed by the idea of the mere complexity just doing the research. (It would certainliy be doable,not with AHK but with my very powerful KEDIT editor (pc version of IBM's mainframe XEDIT--a truly awsome editor). Yes, it would also mean some, coding would need manual handling, and that would not be the easy part, given that, AFAIK, GOTO is gone in V2--and probabliy other commands. Sadly, however, it may be difficult to share such Kedit code, as the software runs about $99. I would have to inquire about running it from a server or website.

guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Need more braces in loop blocks in v2

Post by guest3456 » 27 Mar 2023, 22:27

joefiesta wrote:
26 Mar 2023, 10:56
Converting to V2 - further thought

I would convert to V2 if for only one thing. A DEFINITIVE listing of EVERY syntax change that would be necessary, including V1 syntax and V2 syntax. This would mean I could--and I probably would--write an edit macro to automate the zillions of changes. The appeal of the improved syntax is enticing, but it is overwhelmed by the idea of the mere complexity just doing the research. (It would certainliy be doable,not with AHK but with my very powerful KEDIT editor (pc version of IBM's mainframe XEDIT--a truly awsome editor). Yes, it would also mean some, coding would need manual handling, and that would not be the easy part, given that, AFAIK, GOTO is gone in V2--and probabliy other commands. Sadly, however, it may be difficult to share such Kedit code, as the software runs about $99. I would have to inquire about running it from a server or website.
there is no definitive list

see here, help us:
viewtopic.php?f=6&t=25100


Post Reply

Return to “Bug Reports”