-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.
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.
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.
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.
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)
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)
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