Are optional parameters possible?
simonl
Posts: 866
Hi All,
I'd like to create a method that has optional parameters, like:
Where 'z' is optional; taking a default value if it's omitted in the call. Is this possible?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
BTW: I type as I'm thinking, so please don't take any offence at my writing style
I'd like to create a method that has optional parameters, like:
PUB myMethod( x, y, z )
Where 'z' is optional; taking a default value if it's omitted in the call. Is this possible?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
BTW: I type as I'm thinking, so please don't take any offence at my writing style
Comments
I guess I could, but the programmer would need to remember to add the -1; in which case they may as well just enter the default value!
I should explain; I'm trying to write a beginners tutorial to Propeller communications, and I've just come across something where optional parameters would be useful (in a helper object). With optional parameters, the helper methods can default them to the most commonly used values, but be flexible enough to allow non-common values for the more experienced/needy.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
BTW: I type as I'm thinking, so please don't take any offence at my writing style
methodXY(x,y) <-- This method just calls methodXYZ(x,y,-1)
methodXYZ(x,y,z)
I am not sure how expensive that would be in the overhead of nested calls, but it would make it simpler to understand (from the outside).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
BTW: I type as I'm thinking, so please don't take any offence at my writing style
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
You'll always have as many take-offs as landings, the trick is to be sure you can take-off again
BTW: I type as I'm thinking, so please don't take any offence at my writing style
I think I understand what you are after and frankly I don't think it is possible. You might be able to get around this limit by using a type of global variable. here is an example
LONG ParameterList[noparse][[/noparse]10]
PUB
ParameterList[noparse][[/noparse] 0 ] := x
ParameterList[noparse][[/noparse] 1 ] := y
ParameterList[noparse][[/noparse] 2 ] := z
MethodName(@ParameterList)
Post Edited (Chuck Rice) : 4/12/2009 5:30:15 PM GMT
Here's the approach I use. Given the "parameter" code block below, one can build a method that takes one argument which is an expression of the form: p(v1)+p(v2)+p(vN) and call the "dequeue" in the method to get the input values v1, v2, vN. The number of values are limited to the value of QLEN.
For example here is a method that uses the "parameter" code block (untested).
The "parameter" code block (tested):
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230