I have two Equal method that take these overloads:
TVariantExpression = reference to function(): Variant;
function Equal(const value: Variant): TRuleBuilder; overload;
function Equal(expr: TVariantExpression): TRuleBuilder; overload;
suppose I have another function :
function TForm1.GetMagicNumber: Variant;
begin
Result := 10;
end;
and I invoke function like this:
Equal(Form1.GetMagicNumber);
After inspecting, I get result that second overload is called. Why? because both of them is valid to be called.
In Function overloading, sometimes a situation can occur when the compiler is unable to choose between two correctly overloaded functions. This situation is said to be ambiguous. Ambiguous statements are error-generating statements and the programs containing ambiguity will not compile.
Simply put, overloading is declaring more than one routine with the same name. Overloading allows us to have multiple routines that share the same name, but with a different number of parameters and types.
Form1.GetMagicNumber
is ambiguous. It can be either the function, or the value returned after executing the function. In most contexts, only one of those meanings is valid, and that meaning is chosen.
In your code, either meaning is valid. In such a scenario the language rules mean that the procedural type interpretation is chosen.
To force function invocation write:
Form1.GetMagicNumber()
This is a significant difference from most other languages, e.g. C, C++, C#, Java, Python etc. In those languages you must use parentheses in order to invoke a function.
it is because the first Equal Function have the same type parameter of the second Equal function !
When you do $ ( TVariantExpression = reference to function(): Variant; )
the TVariantExpression
take the Variant type as value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With