Shop OBEX P1 Docs P2 Docs Learn Events
Request/Vote for optional parameters on methods in Spin — Parallax Forums

Request/Vote for optional parameters on methods in Spin

During my practice with the new P2 and using different development tools I have found the optional parameters feature in Spin very helpfully, which can be used with flexspin in FlexProp from Eric R. Smith.

For those users which have not used/seen this feature in the code until now I will short introduce you and will explain this feature. The first code snippet is from the SmartSerial file included in FlexProp.
pub start(rxpin, txpin=-1, mode=0, baudrate=230400)
  if txpin == -1
     baudrate := rxpin
     rxpin := 63
     txpin := 62

  startx(rxpin, txpin, mode, baudrate) 'internal call to initialize the serial

The start method shows 4 parameters, one fix and three optional parameters. The optional parameters can be identified by the equal (=) sign, where it will be initialized with a value if this parameter is not provided on the method call. So, if one would provide all parameters to the start method it would work as a usual method call used in Spin.
But if one leave some of the optional parameters, one can have a different behavior to initialize the serial.

Here are two examples how one can initialize the serial using different number of parameters.
start(115_200) 'start serial on rx-pin 63 and tx-pin 62 with 115_200 baud

start(31, 30)  'start serial on rx-pin 31 and tx-pin 30 with 230_400 baud

As you can see there is only one or two parameters provided to the start method. In the first case it is only the baud rate as the default pins to use are provided by the if-statement in the code of the start method.
On the second call both pins to use are provided on the method call and the baud rate is set as default value of the optional parameter baudrate.

You may mention that you find this implementation complicated but here is another example where is much more advantage in the implementation of a C like printf method using optional parameters. This code snippet is from file std_text_routines included in FlexProp which provides additional methods which can be used with SmartSerial.
pub printf(fmt = string(""), an=0, bn=0, cn=0, dn=0, en=0, fn=0)

This method can use 0 to 6 value parameters to be print out. Here are some examples to use.
printf(string("only text"))                                     'prints only the text

printf(string("a value=%d"), firstValue)                        'prints text and one value

printf(string("decimal=%d, hex=%x"), firstValue, secondValue)   'prints text including two values

And so on up to 6 value parameters.
Without the optional parameter feature one would need to implement 7 methods in Spin to achieve the same functionality, but with different method names as in Spin each method must have a unique name. In other programming languages like Java it is possible to have multiple methods with the same method name as long as the number of parameters or data type of parameters are different between the method.

But this is not possible in Spin. Therefore the optional parameter feature is an easy way to have such flexible methods in Spin as showed with printf. It is able to make code much smaller and more flexible as implementing methods in the usual way.

As we have already some new features in Spin usable with the Propeller Tool/PNut like multiple results and method pointers, I would appreciate if one could use the optional parameter feature with the Propeller Tool too. I know that the development of the Propeller Tool is nearly finished, but I think this feature would be a great benefit to develop flexible and easy reusable Spin objects.

I want also invite other users to show their thought about the optional parameter feature.

Comments

  • JonnyMacJonnyMac Posts: 9,104
    edited 2021-01-09 20:47
    If Chip doesn't want to do it, it's not going to happen -- no matter how many votes you accumulate. FWIW, I'd like method overloading, too.

    I think a higher priority is getting the Spin2 bytecode compiler translated from x86 to generic C so that others could contribute with features like this, and all of the hand-wringing about x-platform compiling can go away. In the end, it's better for Parallax, but -- again -- until Chip wants to do it, it won't happen.
  • JonnyMac wrote: »
    I think a higher priority is getting the Spin2 bytecode compiler translated from x86 to generic C so that others could contribute with features like this, and all of the hand-wringing about x-platform compiling can go away. In the end, it's better for Parallax, but -- again -- until Chip wants to do it, it won't happen.

    Another (probably easier) way would be to add a bytecode-emitting backend to flexspin. That'd then inherit all the nice frontend features for free. If someone wanted to do that, they could start right now. Or throw some cash at Eric to have him do it, I guess.
  • JonnyMac wrote: »
    I think a higher priority is getting the Spin2 bytecode compiler translated from x86 to generic C so that others could contribute with features like this, and all of the hand-wringing about x-platform compiling can go away. In the end, it's better for Parallax, but -- again -- until Chip wants to do it, it won't happen.

    Thank you Jon for clarification and I agree. I was not aware that the Parallax Spin2 compiler does still not exists in C.

    But I'm wondering why there is so little response on this topic? Maybe other users can't see the benefit of this feature or they don't want to use the Propeller Tool.
  • cgraceycgracey Posts: 14,152
    Kaio,

    This is a great idea. I've felt the need for something like this and I really like your examples. As long as the default parameters are constants, this is not too difficult to add to Spin2. I will look into this.

    Thanks for posting this.

    Chip
  • KaioKaio Posts: 253
    edited 2021-01-11 19:40
    Thank you Chip. Yes, the default parameters work as constants if a parameter is not provided/used on the method call. I was also thinking this could be relative easy integrated in the compiler, as the compiler has to generate the method call always with all parameters, using the default values for not provided parameters.
  • This would be fantastic if implemented! I use this feature in FlexSpin quite a bit. Rather than write separate setxx() and getxx() methods, I like to name methods ambiguously enough that they can be interpreted as either. I give the parameter a default value that is invalid/out of range. The method then has a case block that determines if the parameter is valid or not. If it is, it 'sets' the updated value. If not, it 'gets' or returns the current value. That way the code can be written like:
    ' Set:
        sht31.heaterenabled(TRUE)
    
    or
    ' Get:
        if sht31.heaterenabled()
            react_to_it()
    

    Cheers,
    Jesse
Sign In or Register to comment.