Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-10

same routine with different parameters?

12 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

waldo kitty

question: in simple language, how can i have a routine with differing parameters like some routines in FPC and Lazarus do? i've never attempted this before and it is completely alien to my experience but i do use the functionality quite a lot with existing routines...

description: i have a class object with an internal routine that i want to be able to pass no or different parameters to.

example:  procedure MyObject.MyRoutine;
           procedure MyObject.MyRoutine(VarA : SomeType);
           procedure MyObject.MyRoutine(VarA : string; VarB: integer);

at one point, the first instance would be used... at another point one of the other instances would be used with the only difference being the parameters that are passed or not... i know that i would have individual instances with the necessary header and code for each... i just don't know if there is anything else special that needs to be done or if it can be done with an internal class object routine..

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Sven Barth
to waldo kitty
"waldo kitty" wrote
question: in simple language, how can i have a routine with differing

parameters like some routines in FPC and Lazarus do? i've never attempted this before and it is completely alien to my experience but i do use the functionality quite a lot with existing routines...

description: i have a class object with an internal routine that i want

to be able to pass no or different parameters to.

Show quoted text 6 lines
example: procedure MyObject.MyRoutine; procedure MyObject.MyRoutine(VarA : SomeType); procedure MyObject.MyRoutine(VarA : string; VarB: integer); at one point, the first instance would be used... at another point one of

the other instances would be used with the only difference being the parameters that are passed or not... i know that i would have individual instances with the necessary header and code for each... i just don't know if there is anything else special that needs to be done or if it can be done with an internal class object routine..

The most important point to keep in mind us that the parameters need to be different (the result type does not count though!). And you should be careful if you decide to additionally use default parameters for overloaded functions.
Note: In mode Delphi and a few other cases (e.g. cross unit overloads, inheritance) you'll need to add the "overload" directive.

For additional information please look at the language reference guide (can't look up the link currently).

Regards,
Sven

Mark Morgan Lloyd
to waldo kitty: question: in simple language, how can i have a routine with differing parameters like…
waldo kitty wrote 18 lines
question: in simple language, how can i have a routine with differing parameters like some routines in FPC and Lazarus do? i've never attempted this before and it is completely alien to my experience but i do use the functionality quite a lot with existing routines... description: i have a class object with an internal routine that i want to be able to pass no or different parameters to. example: procedure MyObject.MyRoutine; procedure MyObject.MyRoutine(VarA : SomeType); procedure MyObject.MyRoutine(VarA : string; VarB: integer); at one point, the first instance would be used... at another point one of the other instances would be used with the only difference being the parameters that are passed or not... i know that i would have individual instances with the necessary header and code for each... i just don't know if there is anything else special that needs to be done or if it can be done with an internal class object routine..

Just give it a whirl as you've shown, I think you've got it right. Another useful variant is giving *the* *last* parameter an optional value, which will allow you to omit the parameter:

procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1);
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Sven Barth: parameters like some routines in FPC and Lazarus do? i've never attempted this before and…
Sven Barth wrote 14 lines
Am 29.10.2013 18:21 schrieb "waldo kitty" : > example: procedure MyObject.MyRoutine; > procedure MyObject.MyRoutine(VarA : SomeType); > procedure MyObject.MyRoutine(VarA : string; VarB: integer); > > at one point, the first instance would be used... at another point one of the > other instances would be used with the only difference being the parameters that > are passed or not... i know that i would have individual instances with the > necessary header and code for each... i just don't know if there is anything > else special that needs to be done or if it can be done with an internal class > object routine.. The most important point to keep in mind us that the parameters need to be different (the result type does not count though!).

in this specific case, it will be one without any parameters (the original in the 3rd party library) and one with parameters (my needed modifications)...

And you should be careful if you decide to additionally use default parameters for overloaded functions.

hummm... i don't think i need to do that in this case... just having the original unmodified routine for use like always and then my new one with parameters...

Note: In mode Delphi and a few other cases (e.g. cross unit overloads, inheritance) you'll need to add the "overload" directive.

ahhh... i believe that the library i'm using does use mode delphi all over the place... do i add "overload;" to each of the 'duplicate' routines?

For additional information please look at the language reference guide (can't look up the link currently).

i don't even know what to search for... although 'overload' may give me a start...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
waldo kitty
to Mark Morgan Lloyd: Just give it a whirl as you've shown, I think you've got it right. Another useful variant…
Mark Morgan Lloyd wrote 13 lines
waldo kitty wrote: > example: procedure MyObject.MyRoutine; > procedure MyObject.MyRoutine(VarA : SomeType); > procedure MyObject.MyRoutine(VarA : string; VarB: integer); > > at one point, the first instance would be used... at another point one of the > other instances would be used with the only difference being the parameters > that are passed or not... i know that i would have individual instances with > the necessary header and code for each... i just don't know if there is > anything else special that needs to be done or if it can be done with an > internal class object routine.. Just give it a whirl as you've shown, I think you've got it right.

really? that easily?! no way! i was just guessing based on what i've seen in some of the documentation of some routines but i never guessed that it would be that easy to do at any time :)

Show quoted text 5 lines
Another useful variant is giving *the* *last* parameter an optional value, which will allow you to omit the parameter: procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1);

very interesting... will this work with both parameters, too? i don't think i need it in this specific situation, though... i'm mainly wanting to provide the original unmodified routine and my modification of it so i can return something to that 3rd party library development...

FWIW: i decided to try this because i was unable to (hope i'm using the proper terms here) derive a new object from the existing one and override this one inherited routine with my modifications... this would allow me to add my own variables to the object in addition to what it comes with and allow me to have this routine as i need it... but that didn't work for some reason and i've already ripped out all that attempt and gone this route with my modified routine and direct addition of the variable... i just want to be able to provide something back with as little intrusion to the original as possible...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Mark Morgan Lloyd
to waldo kitty
waldo kitty wrote 11 lines
> Another > useful variant is giving *the* *last* parameter an optional value, > which will > allow you to omit the parameter: > > procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1); very interesting... will this work with both parameters, too? i don't think i need it in this specific situation, though... i'm mainly wanting to provide the original unmodified routine and my modification of it so i can return something to that 3rd party library development...

Yes, provided that you don't also try to have a procedure with no parameters. In general, the compiler will tell you if you're doing something silly.

Assume that this is for value parameters only. In particular, I don't think there's a way of defaulting a var parameter to a nil pointer.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
waldo kitty
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 15 lines
waldo kitty wrote: >> Another >> useful variant is giving *the* *last* parameter an optional value, which will >> allow you to omit the parameter: >> >> procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1); > > very interesting... will this work with both parameters, too? i don't think i > need it in this specific situation, though... i'm mainly wanting to provide > the original unmodified routine and my modification of it so i can return > something to that 3rd party library development... Yes, provided that you don't also try to have a procedure with no parameters. In general, the compiler will tell you if you're doing something silly.

hummm... it wouldn't be the first time that i've not groked what the error messages were trying to tell me... that's why i decided to try this vector instead of the failed attempts to create another opbject based on the original and inherit the procedures with a replacement of this particular one...

Assume that this is for value parameters only. In particular, I don't think there's a way of defaulting a var parameter to a nil pointer.

hummm... the original has no parameters at all... my addition has two... will this be a problem? no, i've not had a chance to get back to the development machine and try :?

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.
Sven Barth
to waldo kitty: in this specific case, it will be one without any parameters (the original in the 3rd…
waldo kitty wrote 25 lines
On 10/29/2013 1:56 PM, Sven Barth wrote: > Am 29.10.2013 18:21 schrieb "waldo kitty" : > > example: procedure MyObject.MyRoutine; > > procedure MyObject.MyRoutine(VarA : SomeType); > > procedure MyObject.MyRoutine(VarA : string; VarB: integer); > > > > at one point, the first instance would be used... at another point > one of the > > other instances would be used with the only difference being the > parameters that > > are passed or not... i know that i would have individual instances > with the > > necessary header and code for each... i just don't know if there > is anything > > else special that needs to be done or if it can be done with an > internal class > > object routine.. > > The most important point to keep in mind us that the parameters need > to be > different (the result type does not count though!). in this specific case, it will be one without any parameters (the original in the 3rd party library) and one with parameters (my needed modifications)...

That's valid :)

Show quoted text 7 lines
> Note: In mode Delphi and a few other cases (e.g. cross unit overloads, > inheritance) you'll need to add the "overload" directive. ahhh... i believe that the library i'm using does use mode delphi all over the place... do i add "overload;" to each of the 'duplicate' routines?

Yes.

Show quoted text 8 lines
> For additional information please look at the language reference > guide (can't > look up the link currently). i don't even know what to search for... although 'overload' may give me a start...

It seems that overloading is only mentioned here:
http://www.freepascal.org/docs-html/ref/refse79.html#x163-17300014.5

Regards,
Sven

Sven Barth
to Mark Morgan Lloyd: Just give it a whirl as you've shown, I think you've got it right. Another useful variant…
Mark Morgan Lloyd wrote 27 lines
waldo kitty wrote: > question: in simple language, how can i have a routine with differing > parameters like some routines in FPC and Lazarus do? i've never > attempted this before and it is completely alien to my experience but > i do use the functionality quite a lot with existing routines... > > description: i have a class object with an internal routine that i > want to be able to pass no or different parameters to. > > example: procedure MyObject.MyRoutine; > procedure MyObject.MyRoutine(VarA : SomeType); > procedure MyObject.MyRoutine(VarA : string; VarB: integer); > > at one point, the first instance would be used... at another point > one of the other instances would be used with the only difference > being the parameters that are passed or not... i know that i would > have individual instances with the necessary header and code for > each... i just don't know if there is anything else special that > needs to be done or if it can be done with an internal class object > routine.. Just give it a whirl as you've shown, I think you've got it right. Another useful variant is giving *the* *last* parameter an optional value, which will allow you to omit the parameter: procedure MyObject.MyRoutine(const VarA : string; VarB: integer= -1);

Default paremeters can be used for more than the last parameter. There is however the following caveat: if you have e.g. a function with three parameters and the last two have default values, but you now have a call where you're fine with the default value of the second (overall) parameter, but not with that of the third you'll nevertheless have to specify the second parameter.

Regards,
Sven

Sven Barth
to waldo kitty: really? that easily?! no way! i was just guessing based on what i've seen in some of the…
waldo kitty wrote 9 lines
FWIW: i decided to try this because i was unable to (hope i'm using the proper terms here) derive a new object from the existing one and override this one inherited routine with my modifications... this would allow me to add my own variables to the object in addition to what it comes with and allow me to have this routine as i need it... but that didn't work for some reason and i've already ripped out all that attempt and gone this route with my modified routine and direct addition of the variable... i just want to be able to provide something back with as little intrusion to the original as possible...

Overriding the behavior of an inherited function will only work if that function is a) at least "protected" (or you are in the same unit) and b) this function is declared as "virtual" (see also:
http://www.freepascal.org/docs-html/ref/refsu29.html#x75-850006.3.3 )

Regards,
Sven

Sven Barth
to waldo kitty: hummm... it wouldn't be the first time that i've not groked what the error messages were…
waldo kitty wrote 9 lines
> Assume that this is for value parameters only. In particular, I don't > think > there's a way of defaulting a var parameter to a nil pointer. hummm... the original has no parameters at all... my addition has two... will this be a problem? no, i've not had a chance to get back to the development machine and try :?

If you'd use two default parameters this would be problematic, yes, because a call to this overloaded function could either mean to call the function with no arguments or to call the other function with its two default values. Normally the compiler should then tell you that what you're trying to do is ambigous.

Regards,
Sven

waldo kitty
to Sven Barth: That's valid :) Yes. It seems that overloading is only mentioned here:…
Sven Barth wrote 6 lines
Am 29.10.2013 20:32, schrieb waldo kitty: > On 10/29/2013 1:56 PM, Sven Barth wrote: >> Am 29.10.2013 18:21 schrieb "waldo kitty" : >> > example: procedure MyObject.MyRoutine; >> > procedure MyObject.MyRoutine(VarA : SomeType); >> > procedure MyObject.MyRoutine(VarA : string; VarB: integer);

[...]

Show quoted text 6 lines
>> Note: In mode Delphi and a few other cases (e.g. cross unit overloads, >> inheritance) you'll need to add the "overload" directive. > > ahhh... i believe that the library i'm using does use mode delphi all over the > place... do i add "overload;" to each of the 'duplicate' routines? Yes.

yes, i did have to add "overload;" to both declarations... so far, tests show that this is working as desired... the original with no parameters is called and it calls itself with no parameters like it always has and my version calls itself with parameters as desired... it looks good...

i just wish that i had been able to do the inheritance thing so that i didn't have to modify the original object to add another variable and my custom routine... i guess i should try that again and see why... IIRC, it had to do with an object that is assigned as an internal routine not being registered or such... the compiler message i recall was about registration...

Show quoted text 8 lines
>> For additional information please look at the language reference guide (can't >> look up the link currently). > > i don't even know what to search for... although 'overload' may give me a > start... > It seems that overloading is only mentioned here: http://www.freepascal.org/docs-html/ref/refse79.html#x163-17300014.5

yeah, i found that and i think one other but wasn't too sure i was on a proper track at that time...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.