AutoHotkey Community

It is currently May 26th, 2012, 8:38 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: July 9th, 2009, 3:17 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2009, 6:04 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2009, 8:44 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
1) What's it got to do with dynamic function calls?
2) The proposed syntax conflicts with assignment.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: syntax
PostPosted: July 9th, 2009, 2:16 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
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
Code:
fx(x as 4)...


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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 9th, 2009, 10:14 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
Try this code.
Code:
F(x := 3)
MsgBox % x
F(ByRef A) {
    A += 1
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: conflict
PostPosted: July 9th, 2009, 10:54 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
byref parameters are not local anyways.
The proposed syntax is for non-byref parameters.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 12:56 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 8:41 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Another syntax suggestion:
Code:
fx(local x := 4)

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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 9:44 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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 =.
Code:
fx(x: 4)
(This has probably been suggested before.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 5:32 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 7:26 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
I presented my workaround here and also suggested this around 2 years ago on Developers forum.

Quote:
f(p1, , p3)

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     

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 10th, 2009, 10:38 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 2:00 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 11th, 2009, 3:50 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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 _

:)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: August 24th, 2009, 4:48 pm 
Please update http://rosettacode.org/wiki/Named_parameters#AutoHotkey with details of all the limitations of the work-alike posted.

Thanks.


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 6 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group