| Author |
Message |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 27 January 2006, 20:32 PM Post subject: Function/Subroutine overloading??? |
|
|
Does ZBasic support function/subroutine overloading. I get that impression when
I read the System Library documentation. For example:
GetQueue(queue, var, count) or
GetQueue(queue, var, count, timeLimit, timeoutFlag)
Same subroutine, different paramenter list.
Cam this sort of thing be done with my ZBasic functions/subroutines as well? If so, what are the rules?
Here is a similar example from the Delphi7 help file as well along with the explanation.
I expect C++ allows this as well.
Note that Delphi requires the use of the overload directive.
| Code: |
function Divide(X, Y: Real): Real; overload;
begin
Result := X/Y;
end;
function Divide(X, Y: Integer): Integer; overload;
begin
Result := X div Y;
end;
|
These declarations create two functions, both called Divide, that take parameters of
different types. When you call Divide, the compiler determines which function to invoke
by looking at the actual parameters passed in the call. For example, Divide(6.0, 3.0)
calls the first Divide function, because its arguments are real-valued.
In the Delphi case, the routines must be different in their calling sequence either
by parameter type or number.
Any enlightenment will be appreciated.
Vic |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Location: Austin, TX
|
|
Posted: 27 January 2006, 21:02 PM Post subject: |
|
|
Yes the compiler supports polymorphism (function overloading) but only for system library functions. The classic example is CStr which can take a variety of types as the parameter.
This support was added I believe because it is compatible with BasciX and is also nice for application developers. Adding generic support for user written polymorphic functions would be a great addition but would add significant code/effort in the compiler. |
|
| Back to top |
|
 |
spamiam
Joined: 13 Nov 2005
|
|
Posted: 28 January 2006, 0:44 AM Post subject: |
|
|
In C++, and I suppose in other languages as well, these "overloaded" functions do not really have the same name. In English they have the same name, but to the compiler, one is "the function beginning at address xxxx:xxxx", and the other is the "function beginning at address yyyy:yyyy".
It is a fictional name that seems to you to be the "same".
So, you can have FunctionI() for integer args, FunctionF() for floats, FunctionB() for byte args, and so on. You save the compiler developer many headaches, You still have to know by what types of args you need to pass, which letter to append to the function name. It uses no more space than overloaded functions (since I do not know the exact specific inside compilers, I will amend that statment to read "little more space".)
I have written certain user functions like this for BasicX and ZBasic for a while now. I have not written much in C++ for 15 years, and I really do not miss much of the C++ syntax, except a couple of basic pointer niceties.
Vic, Is there something you really need that polymorphism for that would be very inconvenient to rewrite as FunctionI(), FunctionB(), etc? I admit, that I did write a few polymorphic functions here and there and it was cool!
-Tony |
|
| Back to top |
|
 |
|