Shop OBEX P1 Docs P2 Docs Learn Events
Is there a way to overload a method in SPIN? — Parallax Forums

Is there a way to overload a method in SPIN?

Mike GMike G Posts: 2,702
edited 2008-05-12 11:51 in Propeller 1
Is there a way to overload a SPIN method or supply a variable length argument list?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-11 16:49
    No.

    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)
       call(1,@a)
    PUB call2(a,b)
       call(2,@a)
    PRI call(count,first)
    ' here "count" is the number of arguments
    ' and "first" is the address of the first argument
    
  • ErNaErNa Posts: 1,751
    edited 2008-05-11 19:04
    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 GMike G Posts: 2,702
    edited 2008-05-11 19:58
    Thanks for the suggestions.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-05-12 11:39
    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 GMike G Posts: 2,702
    edited 2008-05-12 11:51
    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.
Sign In or Register to comment.