When passing variables order is changed

I had two really weird bugs today. It was with a Function I adjusted. This is the concept:

getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String
Yet I was able to compile OK and call the function using:

newString = getSomething(1, 2, 3)

I should’t have been allowed to call the Function without defining param5 since it has no default. This is bug #1.

When the Function does run, it sets param1 and param2 OK, but param3 = -1 and param5 = 3, whereas I would expect param3 = 3, param4 = -1 and param5 = nil. This is Bug #2.

Before I submit it into Feedback, am I not understanding something here?

I think your exceptions are wrong.

I think the parameter 3 goes first to fill the required variant.
and than all optionals are filled with given parameters.

It’s a Xojo specialty to have non optional parameters after optionals.

best way is to have required inputs and then optional ones or to put all to be optional and set default values of them.

If you want the method to behave like expected, you have to make sure the parameter requirements are fulfilled.
param5 is not optional, so the compiler knows the last of the specified method properties must be the Variant param5. As the rest is optional and you pass number values, the compiler assigns them as Integers to param1 and param2.

If you want to avoid this apparent parameter skipping, either make param5 optional too like Bogdan said or call the method with

newString = getSomething(1, 2, 3, Nil)

I entirely agree that the syntax of the function is wrong and I normally DO place the required variables first with the optional variables after.

What I was querying was why the IDE didn’t stop this from compiling and why did the middle variable get passed to the final variable.

Because your input fulfills the requirements of your method parameters distinctly:
param1 – param4 have to be Integers, but they are optional. param5 must be present, but it is a variant which can also be an Integer of course. Because it must be present and 3 is a variant too, the compiler assigns 3 to param5, 1 to param1, 2 to param2 and fills the rest with -1s.
It would be different if param5 were an object, a control or anything except a number. Or if it would be optional too. In these cases, the compiler would assign the variables as you expected.

In any case, there is no need for a bug report. Like Christian said, your expectations did not match the way the compiler works, but there is nothing wrong with it.*

  • in this case at least.

one more thing but don’t know if it’s possible with xojo to do is to call by name input variable and after then to set value of that.
for this you should see tech. doc. or to contact some of xojo people.

in your case

getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String

would be something like

getSomething(param5:='some text',param1:=123,param2:=2)

[quote=399218:@Bogdan Pavlovic]one more thing but don’t know if it’s possible with xojo to do is to call by name input variable and after then to set value of that.
for this you should see tech. doc. or to contact some of xojo people.

in your case

getSomething(param1 As Integer = -1, param2 As Integer = -1, param3 As Integer = -1, param4 As Integer = -1, param5 As Variant) As String

would be something like

getSomething(param5:='some text',param1:=123,param2:=2) [/quote]

No, this call form does not exist. Call with params named instead of positional.

@Jason Parsley
Guess one more good thing to add in Xojo in future!

As another example a method from a declare library I published recently:

Protected Function PowerData(node as int32 = 0, msr as int32) As double() // code stripped End Function

I usually specify only the msr parameter, because if I don’t pass two Ints, node will automatically be 0. This is perfectly reliable, although it might be confusing for the user of the library.

EDIT:
This is equivalent to a method override:

Protected Function PowerData(node as int32, msr as int32) As double() // code stripped End Function

Protected Function PowerData(msr as int32) As double() Return PowerData(0, msr) End Function