 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Thu Jul 09, 2009 2:17 am Post subject: passing parameters by name |
|
|
Instead of evaluating then silently discarding extra parameters, it might be more useful to use them as local variables instead. This would allow calling parameters by name.
So instead of the following workaround inolving a global,
| Code: | x = fx
%x%(x := 3)
fx()
{
global x
msgbox % x ; x is 3
}
|
you could do this:
| Code: | ; proposed syntax
x = fx
%x%(a := 3) ; a becomes a local variable automatically
fx()
{
msgbox % a ; a is 3
}
|
|
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Thu Jul 09, 2009 5:04 am Post subject: |
|
|
What is the reason for that? Is it just to omit variables?
I think you would get confused very quickly.
If I have to pass many different variables I use that technique
| Code: | x = fx
%x%("a3") ; a becomes a local variable in loop below.
fx(options="")
{
global
local a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,option
If (options){
Loop,Parse,options,%A_Space%
If (option:= SubStr(A_LoopField,1,1))
%option%:= SubStr(A_LoopField,2)
}
msgbox % a ; a is 3
} |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7291 Location: Australia
|
Posted: Thu Jul 09, 2009 7:44 am Post subject: |
|
|
1) What's it got to do with dynamic function calls?
2) The proposed syntax conflicts with assignment. |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Thu Jul 09, 2009 1:16 pm Post subject: syntax |
|
|
| Lexikos wrote: | 1) What's it got to do with dynamic function calls?
2) The proposed syntax conflicts with assignment. |
Only dynamic function calls support more than necessary parameters currently. Actually maybe nondynamic function calls should also support unnecessary parameters.
perhaps a different syntax to accomplish the same thing to avoid the conflict with assignment.
such as
yes, it would allow you to omit variables... that's what makes it named parameters different from just optional parameters at the end, like what is currently allowed.
Edit:
Actually, I don't see the conflict with assignment. Lexikos could you please explain? |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7291 Location: Australia
|
Posted: Thu Jul 09, 2009 9:14 pm Post subject: |
|
|
Try this code.
| Code: | F(x := 3)
MsgBox % x
F(ByRef A) {
A += 1
} |
|
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
Posted: Thu Jul 09, 2009 9:54 pm Post subject: conflict |
|
|
byref parameters are not local anyways.
The proposed syntax is for non-byref parameters. |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Thu Jul 09, 2009 11:56 pm Post subject: |
|
|
In a related note, I'm wondering if it's hard to implement to allow missing/omitting optional parameters.
| Code: | f(p1 = 1, p2 = 2, p3 = 3, p4 = 4){
...
}
f(p1, , p3) |
|
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Fri Jul 10, 2009 7:41 am Post subject: |
|
|
Another syntax suggestion:
Extending the local keyword at function calls.
And for seans suggestion
| Code: | f(p1 = 1, p2 = 2, p3 = 3, p4 = 4){
...
}
f(p1, A_Default, p3) |
introducing new variable for use in different situations (universal). In example it holds a special content, everytime Ahk see this content it does appropriate action to current context. |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7291 Location: Australia
|
Posted: Fri Jul 10, 2009 8:44 am Post subject: |
|
|
| Sean wrote: | | In a related note, I'm wondering if it's hard to implement to allow missing/omitting optional parameters. | I implemented it in a private build a while back, but was reluctant to add it to AutoHotkey_L. Basically I did this:
- Change load-time validation to allow empty parameters. (I chose to disallow blank parameters at the end of the parameter list since it wasn't useful; there may have also been technical reasons.)
- Change the infix to postfix conversion phase to put blank_token into the postfix array when a comma is encountered in a function's parameter list, directly preceded by another comma. blank_token represented an empty string, but was also a unique (pointer) value used to identify "omitted" parameters.
- Change script function-calling at run-time to assign a parameter its default value if the corresponding token on the stack == &blank_token (i.e. the parameter was ,, omitted).
There didn't seem to be any simple way to disallow it for required parameters, so in that case (or for built-in functions), ,, was treated as ,"",.
If the actual function were known at the point the comma is processed, it could prevent omitting of required parameters and also insert the actual default value instead of requiring the '== &blank_token' check for every parameter of every function call.
| tinku99 wrote: | byref parameters are not local anyways.
The proposed syntax is for non-byref parameters. | It was just an example of why you'd want to perform assignment in-line. It is perfectly legitimate to do that when the parameter is not ByRef. There are also other, less important reasons not to implement syntax which can apply only to non-ByRef parameters.
Why should named ByRef parameters be excluded?
There is an easy way to avoid any conflicts with existing valid syntax: omit =.
(This has probably been suggested before.) |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Fri Jul 10, 2009 4:32 pm Post subject: |
|
|
| Lexikos wrote: | | I implemented it in a private build a while back, but was reluctant to add it to AutoHotkey_L. | OK, fine, my query was a casual one anyway as the workaround is quite simple, just set simple one like "" as the default value then redirect it to the genuine one inside the function. I asked it nevertheless 'cause I sometimes felt ridiculous about I was doing. |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4511 Location: Belgrade
|
Posted: Fri Jul 10, 2009 6:26 pm Post subject: |
|
|
I presented my workaround here and also suggested this around 2 years ago on Developers forum.
I sometimes wish this, but named parameters are much much better idea. The problem with above is that you will soon have "InputBox" symptom:
| Code: | | InputBox(txt,,,,,,,,,,,title) |
About named parameters the problem with assignment can be fixed with literals:
| Code: | | Fun("Name" := Name, "Pos":=6 ) |
since you cant assign to literal.
There are dozens of ways to do it, lets give examples on:
Declare:
| Code: | 1. f(p1,p2="",Name:,Pos:=6)
2. f(p1,p2="","Name","Pos"=6)
3. f(p1,p2="") {
params Name, Pos=6 ;position is important.
}
|
Use
| Code: | 1. f(1,2,Name:A_Script)
2. f(1,2,"Name"=A_Script)
3. Can use either 1 or 2
|
To me, the best looking are 3+1 or 1+1
Here are some examples on 1+1:
| Code: | f(p1,p2="",Name:,Pos:=6){
if Name=
;some code here
}
f(1), f(1,2) ;as usual
f(1, Pos:6) ;valid, p2 will take default
f(Pos:6) ;invalid, p1 is mandatory
f(1, Name: x ? v:k, Pos:A_Index+1)
f(1, 2, Name, Pos) ;not using named params
f(1, 2, Pos) ;mistake, Name is defined on third position
f(1, 2, "My Name", 6) ;all literals
f(1, 2, "My Name", Pos:3) ;mixed, first named parameter by position, second by name.
f(1, 2, Pos:3, Name:"My Name", Pos:6) ;although it could be done so that last pos overrides first one, perhaps error is better idea.
name=Pos
value= My Name
f(1, %Name%:Value) ;dynamic
InputBox, param
fun = f
%f%(j, %Param%:Value) ;dynamic function with dynamic parameter
|
So, in short, function argument list consist of 2 parts, unnamed part that is determined by position and named part. Parts can not be mixed as constructs like the following are meaningless:
| Code: | | f(1,2,Name:55, 7) ; error , positional parameter after named |
_________________
 |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Jul 10, 2009 9:38 pm Post subject: |
|
|
How about that? | Code: | f("time""" . A_Now . """tick""" . A_TickCount . """var""1")
f(v=""){
If (v)
Loop,Parse,v,% """"
o:= !o ? A_LoopField : ((1 and %o%:=A_LoopField ) ? "")
MsgBox % time "`n" tick "`n" var
} |
The only disadvantage is you cannot use literal "" inside your variables.
| Code: | var:="test""1"
f("var""" . var) ;will set var to test instead test"1 |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Sat Jul 11, 2009 1:00 am Post subject: |
|
|
| majkinetor wrote: | | I sometimes wish this, but named parameters are much much better idea. The problem with above is that you will soon have "InputBox" symptom: | That depends on the number of parameters, of course. As I myself needed to skip just 1 or 2 parameters mostly, missing parameter is more handy to me. BTW I'm not against named parameter. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Sat Jul 11, 2009 2:50 am Post subject: |
|
|
| HotKeyIt wrote: | | The only disadvantage is you cannot use literal "" inside your variables. |
er.. I would term it as "Limitation"
I am rewriting my InternetFileRead() and cooked up the following method ( much similar to yours ) for overriding values of static and local variables.
| Code: | #SingleInstance, Force
MyFunc( "Val=0, w=1024, Text=The Quick Brown Fox, newVar=I'm New" )
MyFunc( _overrides="" ) {
Static x=5, y=5, w=100, h=100, Count
Name:="AutoHotkey", Type:="Scripting", Text:="qwerty", Val:=True
Loop, Parse, _overrides,`,=, %A_Space% ; Override routine for Local/Static variables
A_Index & 1 ? (_:=A_LoopField) : (%_%:=A_LoopField)
Listvars
WinWaitClose, %A_ScriptFullPath%
} |
The limitations are:
Var should not contain = or ,
Var name should not be _overrides or _
 |
|
| Back to top |
|
 |
Paddy3118 Guest
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|