bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Overloading and identifying types (numbers and Null)

Open GWRon opened this issue 5 years ago • 5 comments

The latest brl.mod/reflection.mod-change introduced some overloading - leading to issues in my code as the overloading ("of course"?) cannot identify what method to use.

SuperStrict
Framework Brl.StandardIO

Type TTest
	Method PrintOut(v:Int)
		Print "Integer: " + v
	End Method

	Method PrintOut(v:Byte)
		Print "Byte: " + v
	End Method

	Method PrintOut(v:Short)
		Print "Short: " + v
	End Method

	Method PrintOut(v:String)
		Print "String: " + v
	End Method

	Method PrintOut(v:Object)
		If v
			Print "Object: " + v.ToString()
		Else
			Print "Object: null"
		EndIf
	End Method
End Type


Local t:TTest = New TTest
t.PrintOut(Null) 'fails
t.PrintOut(Object(Null)) 'works

t.PrintOut(0) 'odd
t.PrintOut(0:Byte) 'works

First fail results in Compile Error: Unable to determine overload to use: Method TTest.PrintOut(v:Int) or Method TTest.PrintOut(v:Byte). The "odd" print is using the "int" overloaded method. When using the brl.reflection functionality field.Set(obj, 0) it results in Compile Error: Unable to determine overload to use: Method TField.Set:Int(obj:Object,value:Byte) or Method TField.Set:Int(obj:Object,value:Short).

I understand the "problem" behind it - how should the compiler know about the value given to the function. Is a "0" an integer or an "byte"? A "null" can be an empty string, a non existing object, or simply a "0".

During compilation BCC knows if there is a "", an 0 or a null written, so it should be able to change preference: empty strings prefer string-overloads, null prefers objects and a number ... hmm, it could be the lowest possible number (0-255 = byte - and so on) but this hmm... is tricky. Maybe BCC should output a warning for ambiguity then. Or it should print a hint next to the current error message. Something stating that a number can be "defined" by appending the type (0:byte).

What are your thoughts on this?

GWRon avatar Aug 08 '19 08:08 GWRon