%Expr% [BUG] or [_3D_ not understood]

Discuss the future of the AutoHotkey language
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

%Expr% [BUG] or [_3D_ not understood]

Post by _3D_ » 20 May 2018, 15:01

Is next example Error or Bug?

Code: Select all

fun()
fun() {
	a:= 1
	
	;https://lexikos.github.io/v2/docs/Variables.htm#ref
	;%Expr% dynamically retrieves a variable by name.
	
	df(s) {
		return %s%	; <<< a not visible
	}
	
	vf() {
		return a	; <<< a     visible
	}
	
	while a <= 5 {
		msgBox(df("a") " | " vf()), a++
	}	
}
AHKv2.0 use the future now.
Helgef
Posts: 3190
Joined: 17 Jul 2016, 01:02
Contact:

Re: %Expr% [BUG] or [_3D_ not understood]

Post by Helgef » 20 May 2018, 15:12

See here.

Cheers.
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: %Expr% [BUG] or [_3D_ not understood]

Post by _3D_ » 20 May 2018, 15:40

I see - there need action with variable with the main function not just action.

Code: Select all

df(s) {
		__:= 1		; some action but nothing
		return %s%	; <<< a not visible
	}
And ...

Code: Select all

df(s) {
		__:= a		; action with var from main function
		return %s%	; <<< a     visible
	}
Thanks Helgef.

Code: Select all

fun()
fun() {
	a:= 1

	df(s) {
		return %s%	; <<< a not visible but A_Index visible
	}

	while a <= 5 {
		msgBox(df("a") " | " df("A_Index")), a++
}	}
AHKv2.0 use the future now.
Helgef
Posts: 3190
Joined: 17 Jul 2016, 01:02
Contact:

Re: %Expr% [BUG] or [_3D_ not understood]

Post by Helgef » 20 May 2018, 16:02

Built in Variables are available everywhere, you can't declare them (eg local).
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: %Expr% [BUG] or [_3D_ not understood]

Post by _3D_ » 20 May 2018, 16:54

Code: Select all

fun()
fun() {
	deref(__exp) {
		;_:= a ;a action
		;_:= b ;b action
		;_:= c ;c action
		if RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)
			__exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% ""))
		return __exp
	}
	
	a:= 1
	b:= 1
	c:= 1
	while a <= 3
		msgBox(deref("a= %a% | b= %b% | c= %c%")), a+=1, b+=2, c+=3
}
Actually there must be fake action for any variable that need to be visible.
AHKv2.0 use the future now.
_3D_
Posts: 186
Joined: 29 Jan 2014, 14:40

Re: %Expr% [BUG] or [_3D_ not understood]

Post by _3D_ » 21 May 2018, 00:41

I do some tests and conclusion - AHK have main problem with %Expr%.
Test 1: global variables and functions

Code: Select all

deref(__exp) {
	return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	
} 

a:= 5
b:= 5
c:= 5

while A_Index <= 3 {
	msgBox(deref("a= %a% | b= %b% | c= %c%"))
	a+=1, b+=2, c+=3
}
As we expected a, b, c not visible inside function deref.
Test 2: global variables and short functions

Code: Select all

deref(__exp) => RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	

a:= 5
b:= 5
c:= 5

while A_Index <= 3 {
	msgBox(deref("a= %a% | b= %b% | c= %c%"))
	a+=1, b+=2, c+=3
}
In this case a, b, c visible inside short function deref.
Short function deref(__exp) work like MACROS when in global scope.
Test 3:We expected that short function will see variables in the same scope as in Test 2 (but)

Code: Select all

fun()
fun() {
	deref(__exp) => RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	

	a:= 1
	b:= 1
	c:= 1
	while A_Index <= 3 {
		msgBox(deref("a= %a% | b= %b% | c= %c%"))
		a+=1, b+=2, c+=3
}	}
Test 4:Nested function without "some action"

Code: Select all

fun()
fun() {
	deref(__exp) {
		return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	
	} 

	a:= 1
	b:= 1
	c:= 1
	while A_Index <= 3 {
		msgBox(deref("a= %a% | b= %b% | c= %c%"))
		a+=1, b+=2, c+=3
}	}
Test 5:Nested function with "some action"

Code: Select all

fun()
fun() {
	deref(__exp) {
		_:= a ;a action
		_:= b ;b action
		_:= c ;c action
		return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	
	} 

	a:= 1
	b:= 1
	c:= 1
	while A_Index <= 3 {
		msgBox(deref("a= %a% | b= %b% | c= %c%"))
		a+=1, b+=2, c+=3
}	}
Any variable that must be seen - must have "some action".
Test 6:BASIC programming style.

Code: Select all

fun()
fun() {
	a:= 1
	b:= 1
	c:= 1
	while A_Index <= 3 {
		__exp:= "a= %a% | b= %b% | c= %c%"
		gosub derefLABEL
		msgBox(__exp)
		a+=1, b+=2, c+=3
	}	
	return

	derefLABEL:
		if RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var) {
			__exp:= RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")
			goto derefLABEL
		}
	return
}
As we not expected there only 2 working sets: Test 2 and Test 6.
Conclusions:
1. AHK have 3 type of functions.
2. Short Functions and BASIC style worked in global scope like MACROSes.
3. Nested functions not work as we expected.
4. Probably it is main [BUG] how %Expr% work.
AHKv2.0 use the future now.
User avatar
nnnik
Posts: 3330
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: %Expr% [BUG] or [_3D_ not understood]

Post by nnnik » 21 May 2018, 01:07

Can't we just remove %var%?
Recommends AHK Studio
Helgef
Posts: 3190
Joined: 17 Jul 2016, 01:02
Contact:

Re: %Expr% [BUG] or [_3D_ not understood]

Post by Helgef » 21 May 2018, 01:15

There are no bugs here.
fat arrow wrote:The function is assume-local if it is nested inside another function, otherwise it is assume-global.
Regarding Test 4), as pointed out in the link I provided above, you need to refer to the variables non-dynamically inside a nested function in order for them to be visible (free variables). Eg,

Code: Select all

fun()
fun() {
	deref(__exp) {
		return RegExMatch(__exp, "i)[%][a-z_]\w*[%]", __var)? __exp:= deref(RegExReplace(__exp, "i)" __var.Value, %subStr(__var.Value, 2, -1)% "")): __exp	
		(a,b,c)	; Referring to the variables non-dynamically
	} 

	a:= 1
	b:= 1
	c:= 1
	while A_Index <= 3 {
		msgBox(deref("a= %a% | b= %b% | c= %c%"))
		a+=1, b+=2, c+=3
}	} 
@ _3D_, your regex doesn't really cover all possible variable names.

@ nnnik, you could change the syntax, the feature should not be removed.
User avatar
nnnik
Posts: 3330
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: %Expr% [BUG] or [_3D_ not understood]

Post by nnnik » 21 May 2018, 01:31

@ nnnik, you could change the syntax, the feature should not be removed.
why?
Recommends AHK Studio
Helgef
Posts: 3190
Joined: 17 Jul 2016, 01:02
Contact:

Re: %Expr% [BUG] or [_3D_ not understood]

Post by Helgef » 21 May 2018, 01:35

Because then you couldn't refer to a variable dynamically. Why do you want to remove it?
User avatar
nnnik
Posts: 3330
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: %Expr% [BUG] or [_3D_ not understood]

Post by nnnik » 21 May 2018, 02:19

Because it causes inconsistencies with the several types of function definitions. It's also not a requirement for AHK v2 to refer to a variable dynamically. Why would you need this feature?
Recommends AHK Studio
Helgef
Posts: 3190
Joined: 17 Jul 2016, 01:02
Contact:

Re: %Expr% [BUG] or [_3D_ not understood]

Post by Helgef » 21 May 2018, 03:33

You should start a new topic motivating why it should be removed. An example of valid use is for performance when working with strings, variables are much faster than arrays.
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: %Expr% [BUG] or [_3D_ not understood]

Post by guest3456 » 21 May 2018, 08:14

i vote: do NOT remove dynamic double de refs

lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: %Expr% [BUG] or [_3D_ not understood]

Post by lexikos » 23 May 2018, 01:57

nnnik wrote:Because it causes inconsistencies with the several types of function definitions.
How so?
Post Reply

Return to “AutoHotkey v2 Development”