Request/Vote for optional parameters on methods in Spin
Kaio
Posts: 253
in Propeller 2
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.
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.
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.
This method can use 0 to 6 value parameters to be print out. Here are some examples to use.
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.
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
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.
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.
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
or
Cheers,
Jesse
Thanks Chip