Jump to content

Optional Parameters


this-is-me
 Share

How should optional parameters be handled? Note: All examples allow the second parameter to be optional.  

26 members have voted

  1. 1. How should optional parameters be handled? Note: All examples allow the second parameter to be optional.

    • func test($var1, optional $var2) - var2 is equal to @null (new macro)
      4
    • func test($var1, $var2 = "") - var2 is already set to something, if nothing else was specified
      22
    • func test(["value 1", "value 2"]) - UDF would see it as one single parameter and split it. Will not be possible until array declaration syntax is done ([URL=http://www.autoitscript.com/forum/index.php?showtopic=5730]Check here for a discussion[/URL])
      0
    • opt("ExpandVarStrings", 1) $s = Chr(22) func test("value 1$s$value 2") - stretching a little on this one, but can be done now with the unstable
      0


Recommended Posts

Even when I finish the array initialization routine, I do not want to see that third one!

:):)

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...

Link to comment
Share on other sites

@Nutster, Why not?

<{POST_SNAPBACK}>

In short, elegance, or lack thereof. You are wanting to use the array mechanism to pass arguments, but what is expected in the square brackets is values, not formal parameters. It would not be a straight adaptation of the (newly) existing code. It also looks kind of ugly. We still need a nechanism to determine how to know that an optional argument was done or not.

Oh well. It looks like DaveF and JP are working on the C++-style so, the rest of this is moot. Way to go guys!

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...

Link to comment
Share on other sites

I'd vote for a combination of 1 and 2:

func test($var1, optional $var2="")

(to make the code more readable without giving up the option to set a default value)

Any of my own codes posted on the forum are free for use by others without any restriction of any kind. (WTFPL)

Link to comment
Share on other sites

Adding the keyword "optional" seems redundant to me. If a parameter has a default value, its obviously optional. Also, any parameters to the right of an optional parameter also have to be optional, so the keyword would just start taking up space in the function definitions and making them harder to read.

Link to comment
Share on other sites

Valik has a good point here. It makes it obvious that he would be most comfortable in a C-like environment. As the general syntax is simple enough to be BASIC, I wouldn't mind seeing the Optional keyword for internal documentation's sake, but the C developer in me really finds it verbose and as Valik said sort of redundant. I am torn... I think the second one would be best and probably simplest to implement with lower probability of burning already working functionality.

*** Matt @ MPCS

Edited by Matt @ MPCS
Link to comment
Share on other sites

An optional parameter does not neccessarily have to have a default value, but a default value does mean an optional parameter. Plus, you wouldn't have to specify the "Optional" keyword more than once, since all parameters following the keyword must be optional.

My Projects:DebugIt - Debug your AutoIt scripts with DebugIt!
Link to comment
Share on other sites

Klaatu, yes, an optional parameter does have to have a value if the C++ way (Which has been agreed on) is added. Its not optional if it doesn't have some pre-defined value. Also, adding the keyword optional only once before the start of optional parameters is just confusing. There doesn't need to be a keyword, the syntax difference is enough of a distinction to render a keyword redundant.

At any rate. Jon has already said that the C++ way will make it in at some point when he has an opportunity to get around to it. So lets all just take his advice and shut up about this. There isn't any point in discussing it further because Jon has consented to add this sometime and at least 2 people have mentioned they working on it.

Link to comment
Share on other sites

  • Administrators

I'm also not keen on actual optional parameters (rather than default values) as it's recipe for disaster. You can break a function by calling it wrong, or in the function by making assumptions on what was passed. Someone posted a kixtart script earlier and that just has a NULL type value if you miss a parameter out, but you can still do operations on the value which could lead to unexpected results unless at the start of the function you check all the inputs. The c++ way makes all this redundant.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...