[Gambas-user] Method overloading in Gambas

Doriano Blengino doriano.blengino at ...1909...
Thu Feb 5 20:50:58 CET 2009


Benoît Minisini ha scritto:
>> Hello Benoît,
>>
>> On Thu, 5 Feb 2009 16:35:53 +0100, Benoît Minisini wrote:
>>     
>>> It is impossible to implement this kind of method overloading in Gambas.
>>>
>>> [..]
>>>       
>> Ok, understood.
>>
>> I probably found a trick, posting it here so that people looking at the
>> archives can find it :)
>>
>> I'm working (yes, still) on a gb.net.ldap component, and needed this
>> "overloading" thing. I solved it using a Module called "LDAP" (-> static),
>> and a class called LDAPConnection. How to use:
>>
>> ---8<---
>> ldap AS NEW LDAPConnection
>>
>> ldap.Host = ".."
>> ldap.Port = ".."
>> [..]
>> ldap.Connect()
>> --->8---
>>
>> LDAPConnection has:
>>
>> ---8<---
>> PUBLIC SUB Connect()
>>     LDAP.Connect(ME.sHost, ME.iPort, [..])
>> END
>> --->8---
>>
>> And LDAP.Connect() has the right signature and code doing stuff. Obviously
>> one could also call LDAP.Connect() directly.
>>
>> Back to code now!
>>
>> Ciao,
>> David
>>     
>
> You can use optional arguments to replace method overloading.
I think overloading is a nice thing, but by no mean mandatory. Perhaps I 
missed something, but I don't see why one should absolutely have 
overloading (again, can be nice, but everything can be done without 
overloading).

In your example:

> 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
> 

where is the problem in naming those SUBs "Method()" and 
"Method_With_Args(arg1, arg2)"?
There could be a difference if there was a dynamic late-binding, but 
this is a completely different argument...

Regards,

-- 
Doriano Blengino

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list