Optional parameters

NeoTechni

Well-Known Member
Licensed User
Longtime User
[Wish] Optional parameters

This is a biggie,

We need to be able to do

sub Functionname(optional value as int=-1)

end sub

Hell, just assume it's optional if there is a = defaultvalue in there.

It allows for more elegant code, and you could combine the 2 msgbox functions into 1 for example
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
Any response from the devs about this?

Optional parameters would go a long way towards cleaning up code/redundant subs
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I really think this is necessary.

-I'd cut down on redundant subs
For example, the 2 methods to get part of a string (I forget the name) could be combined into 1 which takes 2 parameters, and the second defaults to -1 (an invalid number) so it acts like the single parameter version instead. Using an invalid input as the default to detect when they aren't entered is very convenient

-I have some functions that take like 20 parameters, and most of the time I call it I have to use the default values.
-It also lets you change the default value without changing every time the sub is called

-It lets you add parameters later in the program without having to track down everytime it was called.
 

Tim Forrester

Member
Licensed User
Longtime User
I admit to being a noob but I am in total agreement with you. It is much more difficult to plan a sub without being able to make some parameters optional. When I find another potential usage for a sub and I am missing one parameter to use it, it would be nice to add it on without having to go back and modify all the previous usages. I recognize that is probably sloppy programming but I get paid for quick results, not elegant code.

Thanks for pushing this.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thanks for pushing this.

You're welcome.

But optional parameters are both faster, and more elegant.

For example, in my Star Trek program, I have subs that take a LOT of parameters. Most of them in my Windows version are optional. So a lot of the time, subs in Windows look like

drawbutton(X,Y,Width, Height, ,,, vbBlue)

Where in B4A I'd have to put all 20 of them, even when they are ignored.
 

iancasey

Member
Licensed User
Longtime User
Adding optional parameters would be a great help, this would cut down on the number of intrinsic and user functions and make learning B4A easier.
 

Jost aus Soest

Active Member
Licensed User
Longtime User
OT: No redundant threads by closing the duplicate ones!

He bumped two or three similar threads.

So please close duplicate threads and link to the "original" thread to save all ideas and discussions in one place. This would be very useful for both, developers (e. g. Erel) and users (the rest of us).

As newbie (talking about this forum) I cannot know which threads already exists.

And while reading all the historic threads... I do not always memorize, if I've already agreed to - for me - important topics.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I don't know and it doesn't really matter. He bumped two or three similar threads.

Ah. That explains it. Thank you.
I feel the same way about redundant threads on the forum that I moderate.

But I get his excitement for it too. I use optional parameters quite a bit in VB.
It also makes adding parameters mid-program a lot easier.
I've had to come up with workarounds (using parameters twice)
 
Last edited:

francoisg

Active Member
Licensed User
Longtime User
A great idea!!! Please dev's add this in the next version (along with objects, sharable modules ... :)!!!!!
 

r2d4

Member
Licensed User
Longtime User
I would suggest overloading.

Like:
Sub someFunc(param1 as String)

Sub someFunc(param1 as String, param2 as String)

May be this is easier to implement. Because in Java you cannot set optional parameters.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I would prefer not, as it doesn't solve any of the issues I have with lacking optional parameters.

The solution for optional parameters is simple, when there is a parameter missing, just fetch the default value from the sub declaration in the preprocessor (what converts B4A to java)
 

Jost aus Soest

Active Member
Licensed User
Longtime User
Why overloading is the better optional parameter...

I would prefer not, as it doesn't solve any of the issues I have with lacking optional parameters.
Which issues do you have, that you could not solve by overloading?

The solution for optional parameters is simple, when there is a parameter missing, just fetch the default value from the sub declaration in the preprocessor
The solution for optional parameters is much more complicated: Overloading ("ad-hoc polymorphism") would be a 1-to-1 transition (this can probably solve a simple context-free single-pass compiler), while optional parameters would need a context-sensitive parser.

With overloading you are even more flexible then with optional parameters, as you can use the same name with the same number of arguments for different types. Examples:
B4X:
Sub Print(aInt As int)
Sub Print(aFloat As Float)
Sub Print(aString As String)
Sub Print(aView As View)
Advantages:
- Type safe
- No need for ugly if-then-conditions
- Java-like
 
Top