v66: Overloading Error

Monkey Forums/Monkey Programming/v66: Overloading Error

Goodlookinguy(Posted 2012) [#1]
Edit: Confirmed the bug on v68, look at the next post for a very compact example of the bug.

There's some very strange bug that's causing this overload to fail. It seems to have something to do with the keyword Self, although the error it throws says nothing of the sort.

"Error : Unable to determine overload to use: Function Bug:object(Local obj:object,Local value:Int) or Function Bug:object(Local obj:object,Local value:String)"



Edit:

If I change line 10 to
[monkeycode]Bug(Object(Self), test)[/monkeycode]
then the code works. Although the error had nothing to do with that and the behavior seems incorrect since I've been able to use Self before in a function and not have issues.


Goodlookinguy(Posted 2013) [#2]
I just came to this issue again but it was on a smaller scale which helped me make a much tinier example. Also, this is using monkey v68.

Function Main()
	Bug(New StringList(), 1)
End

Function Bug( obj:Object, value:Int )
End

Function Bug( obj:Object, value:String )
End
Unable to determine overload to use: Function Bug:Int(Local obj:object,Local value:Int) or Function Bug:Int(Local obj:object,Local value:String).



marksibly(Posted 2013) [#3]
This is correct according to Monkey...

* If an overloaded version is found that is an exact match for the number and type of function call parameters, that version is used.

* Otherwise, if there is exactly one overloaded version that can be called by implicitly converting the function call parameters, that version is used.

* Otherwise, an error is generated.

Both 'Bug' overloads require an implicit conversion (ie: at least an upcast of the first parameter to 'object') and neither is an exact match so an error is generated.

More sophisticated overload matching logic could deal with this, but I don't plan on doing that any time soon so you'll have to cast params to object (or, add overloads that are exact matches).


Goodlookinguy(Posted 2013) [#4]
Hmm? This starts getting weird when there's things like this and this is why I filed this as a bug report...

This works.
Function Main:Int()
	Bug(New StringList(), 1)
End

Function Bug( obj:Object, val:Bool )
End

Function Bug( obj:Object, val:Int )
End


This does not work.
Function Main:Int()
	Bug(New StringList(), True)
End

Function Bug( obj:Object, val:Bool )
End

Function Bug( obj:Object, val:Int )
End


It's inconsistent behavior that I would think is considered a bug or at least they should both return the same error.


marksibly(Posted 2013) [#5]
Yep, that *is* weird - should be causing an error. Will take a look...

[edit]
Nope, that works. There is no implicit conversion from int to bool, so there is only one possible match.
[/edit]


Goodlookinguy(Posted 2014) [#6]
I fixed this bug in v78a/b/c/d. It made no sense to have ToString cause a cast to string when a parameter had object as a type that could be given.

In trans/decl.monkey line 541, I changed it from...
If exprTy.EqualsType( declTy ) Continue

to
If exprTy.EqualsType( declTy ) Continue

Local declClass:ClassDecl, parentClass:ClassDecl, found:Bool
declClass = declTy.GetClass()
If exprTy.GetClass() <> Null Then parentClass = exprTy.GetClass().superClass

While parentClass <> Null
	If parentClass = declClass
		found = True
		Exit
	End
	parentClass = parentClass.superClass
End
If found Then Continue


Edit: I tested it quite a bit, it definitely seems fixed. Casting is still done where appropriate and it no longer confuses types. I actually thought this would only fix the object casting problem, but I was wrong. It seems to fix everything.

Edit: I made a pull request here under issue 55.