Sign in to follow this  
Followers 0
this-is-me

Optional Parameters

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

Please sign in or register to vote in this poll.

15 posts in this topic

... Or any other suggestions on optional parameters?


Who else would I be?

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

AutoIt can't handle Null in any string.

So I guess it may be hard to implement the first one.

But, if a new thing will be done I think the 2nd is the best choice by far.

I do not understand the fourth. :) :)

Share this post


Link to post
Share on other sites

@Nutster, Why not?


Who else would I be?

Share this post


Link to post
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...

Share this post


Link to post
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)


It's my job to comfort the disturbed and to disturb the comfortable.

Share this post


Link to post
Share on other sites

Nice idea too.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

My sentiments...


Who else would I be?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

@Klaatu, did you see the question I had to your post in the _IsPressed?

@Matt, good to see you back. Please look in the chat section...

Edited by this-is-me

Who else would I be?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

I like both optiona 1 and 2, but also think that the @NUll macro should be added no matter the outcome.

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