[Wish:] Wish introduce $ syntax to define a Constant

Discuss the future of the AutoHotkey language
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

[Wish:] Wish introduce $ syntax to define a Constant

08 Sep 2023, 00:46

Wish introduce &$ syntax to support creating anonymous and temporary variables.
Sometimes, we need to pass a VarRef to a function with ByRef parameters, but don't want the variable to be changed. For example:
[Code1:]

Code: Select all

f(&x1,&x2,&x3,&x4,&x5){
	x1:=x2:=x3:=x4:=x5:=x1*x2+x3+x4+x5
}
a:=b:=c:=d:=e:=1
f(&a,&$b,&c,&d,&$e)
f(&$e,&c,&$b,&$a,&d)
f(&a,&b,&c,&$d,&e)
; .......................
But currently, to make variables a,b,c,d,e unchangeable, we have to write like [Code2]. And [Code2] is totally equivalent to [Code1].
[Code2:]

Code: Select all

f(&x1,&x2,&x3,&x4,&x5){
	x1:=x2:=x3:=x4:=x5:=x1*x2+x3+x4+x5
}
a:=b:=c:=d:=e:=1
b1:=b,e1:=e
f(&a,&b1,&c,&d,&e1)
e2:=e,b2:=b,a2:=a
f(&e2,&c,&b2,&a2,&d)
d3:=d
f(&a,&b,&c,&d3,&e)
; .......................
Without this feature, we have to give every "unchangeable" variable a different alias every time func is called. After assigning each "alias variable", we should pass them to func. Obviously, naming again and again is boring and trivial. If someone doesn't come up with names for each variable each time, forcing them to give it a meaningless name is not doing anyone any favors.
But this feature can solve it. Every time we use &$, script will assign the right hand to a temporary variable without naming. The refs of $Var1 are different each time $Var1 is occurred. So we call them anonymous variable or temporary variable.
Also sometimes, we need simply pass values such as string or integer, to ByRef parameters just to make it run.
In this code below, $7... represents an anonymous and temporary variable.
[Code3:]

Code: Select all

f(&x1,&x2,&x3,&x4,&x5){
	return x1:=x2:=x3:=x4:=x5:=x1*x2+x3+x4+x5 
}
f(&$7,&$5,&$7,&$8,&$3) ; It will return 7*5+7+8+3=53
Without this feature, we have to produce five meaningless names for each integer above.
[Code4:]

Code: Select all

val:=0
f2(x,y)=>outputdebug(x)
f2(Var, $Var++)
;Var will not be influenced by Var++ this time and pass 0.
As we can see, introducing this &$ syntax will become much more convenient. 😉
Last edited by V2User on 10 Sep 2023, 07:18, edited 4 times in total.
Michaohneel
Posts: 7
Joined: 07 Feb 2021, 16:37
Contact:

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

08 Sep 2023, 11:46

Hey, are you perhaps confusing “anonymous” with “immutable”?

Anonymous means it has no name, but when doing &$b etc. you are clearly still using a name, so idk what is supposed to be anonymous about that.

In case I'm right and you're talking about immutability, this feature wouldn't make a lot of sense though. The whole entire reason a function might declare a parameter as accepting a VarRef is because it wants to be able to mutate the value so it's visible to the outside. That couldn't work when passed an immutable reference of sorts, be it with the special syntax you are suggesting, or by passing a literal value.

What is feasible, however, is a function declaring that a passed VarRef will not be mutated. The other, somewhat common, reason for passing by reference is performance, and this would allow you to keep the performance gain you get from passing large objects by reference, while still potentially allowing the use of literal values or return values of functions as arguments.

Anyway, this is all assuming you simply confused the two terms, but maybe you didn't. Either way, I think more explaining is needed.
Come talk to me over on the Discord server :)
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

08 Sep 2023, 12:54

Sometimes, we need to pass a VarRef to a function with ByRef parameters, but don't want the variable to be changed.
I do not agree with this, at least when it comes to functions in the standard library. If it's a third-party library it could be modified until this is not the case anymore, and if it is your own code I would not write it in a way that makes this true.

But I will proceed assuming that it is necessary to have this functionality.

The goal is to write a function call like someFunc(&a := otherVariable) where you do not want modifications to otherVariable or care about the output given to a to, yeah? And you do not want to have to burn a dummy variable name.

I do not think this requires new syntax, though if it did I'd recommend against the &$ syntax unless there's a similar construct in another language that has already established that as a convention. Instead, I'd recommend to add a constructor to the VarRef class, which you can do yourself like this:

Code: Select all

#Requires AutoHotkey v2.0

VarRef.Call := (this, val) => &v := val

ref1 := VarRef("Alpha")
ref2 := VarRef("Beta")
ref3 := VarRef("Charlie")

%ref2% := "Bravo"

msgbox (
	Type(ref1) ": " %ref1% ", "
	Type(ref2) ": " %ref2% ", "
	Type(ref3) ": " %ref3%
)
With this added constructor, you can create a variable reference to an effectively anonymous variable, that is initialized to a given value.

Code: Select all

f(&x1,&x2,&x3,&x4,&x5){
	x1:=x2:=x3:=x4:=x5:=x1*x2+x3+x4+x5
}
a:=b:=c:=d:=e:=1
f(&a,VarRef(b),&c,&d,VarRef(e))
f(VarRef(e),&c,VarRef(b),VarRef(a),&d)
f(&a,&b,&c,VarRef(d),&e)
; .......................
Following the rest of the language conventions, it is clear that this creates a Variable Reference by value, as the parameter given to VarRef is not itself a reference. It does not require new syntax, and achieves the same result. If you believe this is too many characters, you can define a function by a single letter name to reduce the total number of characters.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

08 Sep 2023, 14:42

Michaohneel wrote:
08 Sep 2023, 11:46
Either way, I think more explaining is needed.
I have supplemented my post now. And it may be sufficient to explain this time.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

08 Sep 2023, 14:45

geek wrote:
08 Sep 2023, 12:54
Sometimes, we need to pass a VarRef to a function with ByRef parameters, but don't want the variable to be changed.
I do not agree with this, at least when it comes to functions in the standard library. If it's a third-party library it could be modified until this is not the case anymore, and if it is your own code I would not write it in a way that makes this true.

But I will proceed assuming that it is necessary to have this functionality.

The goal is to write a function call like someFunc(&a := otherVariable) where you do not want modifications to otherVariable or care about the output given to a to, yeah? And you do not want to have to burn a dummy variable name.

I do not think this requires new syntax, though if it did I'd recommend against the &$ syntax unless there's a similar construct in another language that has already established that as a convention. Instead, I'd recommend to add a constructor to the VarRef class, which you can do yourself like this:

Code: Select all

#Requires AutoHotkey v2.0

VarRef.Call := (this, val) => &v := val

ref1 := VarRef("Alpha")
ref2 := VarRef("Beta")
ref3 := VarRef("Charlie")

%ref2% := "Bravo"

msgbox (
	Type(ref1) ": " %ref1% ", "
	Type(ref2) ": " %ref2% ", "
	Type(ref3) ": " %ref3%
)
With this added constructor, you can create a variable reference to an effectively anonymous variable, that is initialized to a given value.

Code: Select all

f(&x1,&x2,&x3,&x4,&x5){
	x1:=x2:=x3:=x4:=x5:=x1*x2+x3+x4+x5
}
a:=b:=c:=d:=e:=1
f(&a,VarRef(b),&c,&d,VarRef(e))
f(VarRef(e),&c,VarRef(b),VarRef(a),&d)
f(&a,&b,&c,VarRef(d),&e)
; .......................
Following the rest of the language conventions, it is clear that this creates a Variable Reference by value, as the parameter given to VarRef is not itself a reference. It does not require new syntax, and achieves the same result. If you believe this is too many characters, you can define a function by a single letter name to reduce the total number of characters.
Thanks for your answer. It is really an idea. And I couldn't come up with it at that moment. 😂
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish introduce $ syntax to define a Constant

10 Sep 2023, 07:17

@lexikos @geek @Michaohneel
If var$ syntax is used to define a constant, will it be more useful?
Then, Const1:=$"123" or Const1:=$var1 will be totally the same as I posted above, except that an assignment to Const1 including Const1++ will throw an error. But &Const1 will return a different ref of a temporary variable copied from Const1, everytime &Const1 is used.
Therefore, the value of Const1 will never change.
Currently, only &S and := $ is allowed. Otherwise, it will throw, in order to reserve $ for future use.
Last edited by V2User on 10 Sep 2023, 08:57, edited 1 time in total.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

10 Sep 2023, 08:07

I don't care for this idea, or see the value in considering or debating it.

Michaohneel wrote:
08 Sep 2023, 11:46
The other, somewhat common, reason for passing by reference is performance, and this would allow you to keep the performance gain you get from passing large objects by reference,
VarRef and &Ref are for passing variable references.

Objects are never copied or passed by value, unless you consider the value to be a reference to the object.

There is no performance gain to be had by using & to pass an object. In fact, you may just be adding overhead.

Passing a reference to a variable which contains a string avoids the need to copy the string, in current versions. That won't always be the case. I'm not going to add new operators on the basis of optimizing string passing.


@geek

Code: Select all

VarRef.Call := (this, val) => &val
There's no need for the additional variable (v :=).

If you are defining a separate function, vr(a) => &a is sufficient, or (a=>&a)("value") to define and call.
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: [Wish:] Wish introduce &$ syntax to support creating an anonymous variable

10 Sep 2023, 08:38

lexikos wrote:
10 Sep 2023, 08:07


Objects are never copied or passed by value, unless you consider the value to be a reference to the object.

There is no performance gain to be had by using & to pass an object. In fact, you may just be adding overhead.

Passing a reference to a variable which contains a string avoids the need to copy the string, in current versions. That won't always be the case. I'm not going to add new operators on the basis of optimizing string passing.
For a more explain.
Objects and strings will never be copied when use &$v1, but it will act as v2:=v1 instead.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 141 guests