Are optional parameters possible?
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
var byte default main default := -1 some pub (x , y ) Case default -1 : do woek other : do other workLONG 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).
OBJ disp : "tv_text" PUB main printv( p("s")+p("i")+p("m")+p("o")+p("n")+p("!") ) repeat PUB printv(arg) | value ' print values passed as sum of p(vN) repeat disp.dec(dequeue()) disp.out(" ") while count disp.out($d)The "parameter" code block (tested):
PUB p(v) {{ This is the "parameter" method. The user will string together a list of method calls to push parameters onto the print queue. For example: snprintf(@buf, len, string("values %d %d %d\n"),p(1)+p(2)+p(3)) This will print "values 1 2 3\n" to the buffer. @param v - value that will replace the printf % format specifier. @returns 1. Using a list of p(v)+p(v)+p(v) as the arg to snprintf gives arg count. }} enqueue(v) return 1 CON QLEN = $3f ' up to 31 parameters VAR long queue[noparse][[/noparse]QLEN+1] long tail, head PRI enqueue(val) long[noparse][[/noparse]@queue+head] := val head+=4 head&= QLEN PRI dequeue : rc rc := long[noparse][[/noparse]@queue+tail] tail+=4 tail&= QLEN return rc PRI count if head => tail result := head-tail else result := head+(QLEN-tail) PRI flush head := tail▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230