Compiler not recognising overloaded method

Spoiler warning - this might be a senior moment.

I have 4 overloaded methods in a module mUnits, all are Protected

Screenshot 2023-02-08 at 10.42.19

When I try to call one of these via:

Var value As Double = s.ToDouble
If value.IsNotANumber Then
  //todo message
  Break
Else
  Var u64 As Integer = cCol.units  //vCell.baseValue is a double
  vCell.baseValue = mUnits.convertUnitToBase(value, u64)
End If

Compiler tells me Error: There is more than one method with this name but does not match any of the available signatures.

I’ve seen this compiler error before when there are no methods with that name - but doesn’t appear to be the case this time. Also there are no other methods anywhere with the same name.

What is this type. If it isn’t a double then they’re right. Unless you wrap the call in str() or format(), assuming a string?

Now I see your note about the type.

I’m also assuming that mUnits is the module or class these methods are part of?

Now, I notice you say they are protected, which would mean they are only accessible from within their module?

Yes its a double.

Further investigation shows the compiler can’t differentiate between a double and a desktopCheckbox.visualstates enumeration. Bug?

Work-around is to add an additional dummy parameter to one of the methods.

No, protected means they are accessible by reference to the module. Private means accessible only within the module.

enums are integer so yes, it should be able to tell the difference between an “integer, integer” call and a “double, integer” call.

Actually I don’t think so. Yes they can be typed as an integer but I can happily overload multiple methods with different enumerations, so the compiler should be able to differentiate easily.

Yes, but the base type is always integer. So certainly not double, so there shouldn’t be confusion between “enum, integer” and “double, integer”. I’m saying it should be a bug as you are passing “double, integer” which should be recognises as the last of your options.

i had the same issue today where i had 1 method returing Dictionary and another same name method returing a custom class say myClass.

Some how the compiler would not recognize the return value difference (anymore)?

Var myVariant As variant = myMethod("") // returns dictionary
Var myInstance as MyClass = myMethod("") // returns myClass

same compiler error

Would that be because a variant could also be your myClass?

You can’t overload on the return value, only the parameters.

2 Likes

The best you can do with return is to return a variant. You would still need to cast it to the type expected in the call though. So your class assignment would have to cast to the class.

I can’t reproduce this. I added two methods to a window, one that takes a double, the other that takes an enum, and the compiler has no trouble telling the difference even if I supply an integer instead of a double.

Is your enum a Binary?

Ah, found it I think - though the compiler message led me astray…

The enum in one of the methods should have been desktopCheckbox.visualStates rather than desktopcheckedStates.visualstates. My typo in updating to the new APIs (ugh).

So even though the method being referred to was correctly typed (ie double, integer) the compiler seemed to confuse it with a nonexistent class (desktopCheckbox.visualStates, integer)

Yip, definitely a Senior Moment.

1 Like

Well i have even tried to change it to strict but won’t budge.

So you are saying you can’t have the same function name (same parameters) with a different return value (including in a module) ? Or by overload do you mean, subclassing?

Same function name within the same scope with the same parameter count and types, but different return types, will not work.

// Like this...
Function M1(d As Double) As Dictionary
Function M1(d As Double) As Integer

Best to give these functions distinct names, e.g., ConvertToDictionary.

1 Like

somehow i thought this was always possible before…
i see now, are we sure did didn’t just change somehow?

Var d As Dictionary = functionReturningClass // Return type = Dictionary
Var m As MyClass = functionReturningClass // Return type = MyClass

Both return different types and they won’t compile yeah.
Maybe there should be a notification when you have same method and do this, since you can just have as many methods as you wish doing this. but nowhere the ide tells you there is some issue (unless you build and there is one being called)

I don’t recall a time when this wasn’t true.

As someone who’s becoming more prone to “senior” moments, I gave up with overloading many moons ago. Instead I’ve found using more descriptive names for functions helps me a lot.

convertDesktopCheckedStatesToBase( cb as desktopCheckedStates.visualStates, u64 as integer ) as double
convertDateToBase( d as date ) as double
convertStringToBase( s as string, u64 as integer ) as double

When you use “u64” for the property name, do you mean a Uint64? Xojo’s “Integers” are signed, so this may cause problems.

Now that the ide will show the signatures of overloaded methods, I’m less hesitant.