Sign in to follow this  
Followers 0
jlandes

Optional Keyword For Function Parameters

16 posts in this topic

Is there any way to get an Optional keyword added to function parameters? This would make things much nicer for the standard library. I'd be very greatful!

_Function( $Parm1, Optional $Parm2 )


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites



I would very much like to see this as well.

Share this post


Link to post
Share on other sites

How many optional arguments are you looking for? Just one at the end, or as many as wanted at the end? How can we tell if the optional arguments are supplied or not? What about a default value, like C++?

C++ stores seperate signatures for each function, including using optional parameters. I wonder if a mechanism like this could be used.

Func MyTest(ByRef $Jake, Optional $d1, Optional $e=2.71828182845)

Could you use ByRef and Optional on the same argument?


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

I would like to be able to use as many optional arguments as necessary. I also like the default value like C++ idea. What do you think Brian? Also, I think ByRef and Optional should be able to be used together. Thx.


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This feature would be great!

In case "Optional ByRef $var" or "ByRef Optional $var" is too difficult to parse, perhaps you could introduce the single additional keyword OptionalRef.

Would the number of parameters a UDF receives be available to the AutoIt user?

The only reason I ask is because, currently, the GUIMsg function has different behavior if it has one parameter versus zero parameters.

Imagine this sample AutoIt script:

Func myGuiMsgWrapper( Optional $x)
  If NUMBER_OF_PARAMETERS = 0 Then
    Return GuiMsg()
  Else
    Return GuiMsg($x)
  EndFunc

Could this example be a major problem? Default values would not even overcome it, as far as I can tell :D

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

If I remember correctly, JP had a version of this in his version of AutoIt some time ago. I think the VB way of doing this would be sufficient. Possibly add a macro called @NumParams or something.


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

Well, in C++ the only way to use a function like that is:

foo(int bar = 0)

So, even if no argument is passed, you are guaranteed that your function will receive some meaningful data as the optional parameter.

In AutoIt, I would suggest that if there is an optional keyword, it be like C++ and require a default value to avoid problems (Note: Because of the variants, though, it could be possible to circumvent that requirement, but I still think it would lead to less script errors if the default was required). That translates your example into:

Func foo($bar = "NOTHING!")
    If $bar = "NOTHING!" Then DoNothing()
EndFunc

Of course, AutoIt could also secretly create an $argc variable local to each function, but that is less desirable, I think.

Share this post


Link to post
Share on other sites

If I remember correctly, JP had a version of this in his version of AutoIt some time ago.  I think the VB way of doing this would be sufficient.  Possibly add a macro called @NumParams or something.

You have a good memory it was in the dbg-bastard stuff.

If it is sufficient I can resubmit it to JON :D

Share this post


Link to post
Share on other sites

Can you tell me what any, if any limitations there are with yours, and how to use it within the script syntax? If my memory serves me right, I believe that it is just what I was looking for, but I'd like to be sure. Maybe, you could copy/paste the documentation for that feature. Thanks.


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

#10 ·  Posted

I believe it was just an optional keyword and the function got a variable for the number of parameters passed. I didn't like it for the reasons Valik mentioned above (some variables wouldn't be given values, introduces odd bugs).

The c++ optional way seems to only logical way to do it, but, it would be difficult to code for a tiny gain.

We can all understand things like:

Func test($var, optional $var2 = 10)

But the parsing of "$var2 = 10" would be tricky and usually when an assignment is made just the general "expression parser" code is used and that is designed for complex expression. This would pretty nasty as you could end up with:

Func test($var, optional $var2 = myfunc($10, "test", Random()))

etc... Then you would have to write a wedge of code to limit it (or even create a entirely seperate parser) which is a lot of work just to be able to do:

test($myvar)

instead of

test($myvar, 10)

Share this post


Link to post
Share on other sites

#11 ·  Posted

I believe it was just an optional keyword and the function got a variable for the number of parameters passed.  I didn't like it for the reasons Valik mentioned above (some variables wouldn't be given values, introduces odd bugs).

Are you saying that a macro like @NumParams would cause problems in the context of user-defined functions? Is there an issue regarding local variable creation?

Example idea code:

Func myMouseMoveWrapper($x, $y, Optional $speed)
   Local $speed
   If @NumParams = 2 Then $speed = 10
   Return MouseMove($x, $y, $speed)
EndFunc

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

#12 ·  Posted

No, but the user could get themselves in lots of trouble by forgetting to give the optional variable a value (which can't happen using the C++ way as all optional variables are given a value)

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

In C++, the default values must be constant expression (evaluated at compile time). For autoit it also makes sense to limit default values to "literals", i.e. a literal number or a string. Wouldn't that be fairly easy to scan, by using only the Lexer_String() and Lexer_Number() functions?

The "Optional" keyword is not needed, because the "=" after the argument would indicate that it was optional (however, it may be clearer).

Func myMouseMoveWrapper($x, $y, $speed = 10)

Return MouseMove($x, $y, $speed)

EndFunc

The parser should simply look for "=" after a variable argument, then try to lex the value with the above mentioned functions (otherwise syntax error).

As for build-in functions, all subsequent arguments are required also to be optional, i.e. have default values.

/PS: Although it may be a bit work for something that is really just a convenience, people will always request user definable constructs to be as powerful as the builtin ones. (which I guess is understandable).

Edited by tylo

blub

Share this post


Link to post
Share on other sites

#14 ·  Posted

In C++, the default values must be constant expression (evaluated at compile time). For autoit it also makes sense to limit default values to "literals", i.e. a literal number or a string. Wouldn't that be fairly easy to scan, by using only the Lexer_String() and Lexer_Number() functions?

The "Optional" keyword is not needed, because the "=" after the argument would indicate that it was optional (however, it may be clearer).

Func myMouseMoveWrapper($x, $y, $speed = 10)

  Return MouseMove($x, $y, $speed)

EndFunc

The parser should simply look for "=" after a variable argument, then try to lex the value with the above mentioned functions (otherwise syntax error).

As for build-in functions, all subsequent arguments are required also to be optional, i.e. have default values.

/PS: Although it may be a bit work for something that is really just a convenience, people will always request user definable constructs to be as powerful as the builtin ones. (which I guess is understandable).

The way you have outlined would be perfect. Can this be done and is it too much work? It would be great for some functions that have a parameter that could be optional and, if not specified, defaults to a predetermined value set by the author of the function.

For example:

Func _FunctionName( $sParam1, Optional $sParam2 = "Yes" )
  ... Do function stuff here.
EndFunc

Of course, the keyword "Optional" could be optional. :D I'd be quite happy being able to do something like:

Func _FunctionName( $sParam1, $sParam2 = "Yes" )
  ... Do function stuff here.
EndFunc

Thanks.


Sincerely yours,Jeremy Landesjlandes@landeserve.com

Share this post


Link to post
Share on other sites

Jon, don't want to nag you about this, but do you still think it's not feasable to do this? I could probably look into it if you don't want to.


blub

Share this post


Link to post
Share on other sites

I've not given it any thought to be honest. I want to wrap up the changes for the current unstable version first :D Quite a few changes...lots of stuff to go wrong...

3.0.102 (5th May, 2004) (Beta)

- Changing at the moment: function call code (internal)

- Changed: File dialogs set to use NULL for parent window.

- Changed: "Allow Decompilation" option added to Aut2Exe

- Changed: Aut2Exe gives feedback during compilation and gives the option to abort

- Changed: Compiled scripts much faster when using FileInstall and medium/large files

- Added: Decompiler

- Added: PixelChecksum()

- Changed: Max number of user function limits removed

- Changed: Max number of variable limits removed

- Changed: -1 does no sleep at all for Send/Mouse delays (SendKeyDelay, etc...)

- Fixed: Hotkey registering logic

- Added: Strings starting with "0x" are converted from hex when used numerically

- Added: Multi-directory #include <> paths

- Changed: Multi-line tooltips for ToolTip()

- Changed: Optional occurance parameter added to StringInStr()

- Changed: Optional default filename added to FileOpenDialog() and FileSaveDialog()

- Added: The Au3Gui code

- Added: MouseWheel()

- Added: FileRecycleEmpty() (IE4+ required)

- Added: RegEnumKey() and RegEnumVal()

- Changed: Registry functions accept 4 letters keys (HKLM etc)

- Changed: Remote registry keys are accepted in the form "\\computername\key\subkey" (works for NT/XP/2000 needs additional software on 9x)

- Changed: AutoItSetOption returns the previous setting

- Fixed: Some keywords incorrectly allowed after a THEN keyword

- Changed: (Internal) String functions speeded up (concat in astring and variants)

- Changed: (Internal) ControlSearch modified to include the windowsearch

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0