[Gambas-user] Method overloading in Gambas

Benoît Minisini gambas at ...1...
Thu Feb 5 16:35:53 CET 2009


> Hello,
> other languages (C# and Java can do this, possibly also others) have the
> ability to "overload" methods. Quick example:
>
> ---8<---
> PUBLIC SUB Method()
>     ' Arg1 and Arg2 are properties, which could be set with Arg*_Write()
>     ' methods, or can be default values, or ...
>     Method(Arg1, Arg2)
> END SUB
>
> PUBLIC SUB Method(Arg1 AS Variant, Arg2 AS Variant)
>     [..]
> END SUB
> --->8---
>
> (obviously, if one calls Method(), he should've set Arg1 and Arg2 already)
>
> This is easily doable using a "helper" sub, like:
>
> ---8<---
> PUBLIC SUB Method()
>     Arg1 = [..] ' get these from somewhere
>     Arg2 = [..]
>     DoStuff(Arg1, Arg2)
> END SUB
>
> PUBLIC SUB Method(Arg1 AS Variant, Arg2 AS Variant)
>     DoStuff(Arg1, Arg2)
> END SUB
>
> PRIVATE SUB DoStuff(Arg1 AS Variant, Arg2 AS Variant)
>     [..]
> END SUB
> --->8---
>
> (note the redundancy in method signatures...)
>
> Benoit, is it possible to have this feature in gambas3?
>
> Ciao,
> David

It is impossible to implement this kind of method overloading in Gambas. 

To implement it, the compiler needs to always know the signature of a method 
before calling it. 

But the Gambas compiler cannot, because each class is compiled completely 
independently from others. All linkage is done by interpreter at runtime.

Otherwise, the interpreter would have to do type matching at runtime to choose 
what method to call. This is a waste of time and code, and this is the reason 
why C++, Java... do that at compilation time and not at run time.

Regards,

-- 
Benoît




More information about the User mailing list