View Full Version : Is there a way to overload a method in SPIN?

Mike G
05-11-2008, 11:24 PM
Is there a way to overload a SPIN method or supply a variable length argument list?

Mike Green
05-11-2008, 11:49 PM

If you want to supply a variable length argument list, you could pass the address of an array of pointers, possibly with an argument count as the first element. You could have several short "stub" routines with declared argument lists of various lengths and pass an argument count and the address of the first argument to your main routine since the actual parameters are pushed in order into the stack with the first being lowest in memory like:

PUB call1(a)
PUB call2(a,b)
PRI call(count,first)
' here "count" is the number of arguments
' and "first" is the address of the first argument

05-12-2008, 02:04 AM
I had exactly this problem: Somewhere in global memory I store variable and I wish to have access to these variables from other COG's without changing the argument list. I decided to pass an array of pointers to the start routine. The first referenced value allows to send a command to the process and with help of a little synchronising mechanism it is possible to activate different functions running parallel. The propeller is just great! http://forums.parallax.com/showthread.php?p=725595 It should be possible to understand the code without understanding german. But I'm ready to translate if there is some resonance

Mike G
05-12-2008, 02:58 AM
Thanks for the suggestions.

05-12-2008, 06:39 PM
If you really, really need to be able to change the method called it is possible but a pain in the neck and all the different methods will need to have the same number of variables. The easiest thing to do is something like this

PUB callMethod(method,var1,var2....)
switch method
METHOD1: doSomething(var1,var2...)
METHOD2: doSomethingElse(var1,var2...)

PUB doSomething(variables)
some code

PUB doSomethingElse(variables)
more code

It is possible to play around with the pointers in the method table as well but it takes a fair bit of work. If you want I can try to explain it.

Mike G
05-12-2008, 06:51 PM
I appreciate everyone's help. It's much less work if I change my coding strategy. I guess using a memory pointer and length, like passing a variable length string, should do the trick just fine.