|
Joe,
You can take the %addr() of a CONST parm...but only if comparing it to
*NULL. In other words, the compiler will allow
if %addr(iParm1) = *NULL;
but it won't allow
myPtr = %addr(iParm1);
But you only need to do that if the parm is options(*OMIT)
with options(*NOPASS), all you need is %parms().
I'd probably do LogCommon() with all parms as options(*OMIT)
then for instance, in LogCode() do
select;
when %parms() = 2;
LogCommon(*OMIT:code:*OMIT:*OMIT:*OMIT);
when %parms() = 3;
LogCommon(*OMIT:code:parm1:*OMIT:*OMIT);
<ect>
endsl;
Charles
On Tue, Apr 2, 2019 at 2:13 PM Joe Pluta <joepluta@xxxxxxxxxxxxxxxxx> wrote:
Every time I get close to a solution, I get whacked.
Here's my simple case. I want to write this procedure:
dcl-proc LogData;
dcl-pi *n;
iTYPE like(dsLOGDTA.L0TYPE) const;
iCODE like(dsLOGDTA.L0CODE) const;
iPARM1 like(dsLOGDTA.L0PARM1) const options(*nopass);
iPARM2 like(dsLOGDTA.L0PARM2) const options(*nopass);
iPARM3 like(dsLOGDTA.L0PARM3) const options(*nopass);
end-pi;
It's really pretty simple. A type, a code, and up to 3 optional
parameters, all of which can be literals or expressions. The above
works. However, I'd like to encapsulate those further so users can call
them without the type and code, like so:
dcl-proc LogCode;
dcl-pi *n;
iCODE like(dsLOGDTA.L0CODE) const;
iPARM1 like(dsLOGDTA.L0PARM1) const options(*nopass);
iPARM2 like(dsLOGDTA.L0PARM2) const options(*nopass);
iPARM3 like(dsLOGDTA.L0PARM3) const options(*nopass);
end-pi;
dcl-proc LogType;
dcl-pi *n;
iTYPE like(dsLOGDTA.L0TYPE) const;
iPARM1 like(dsLOGDTA.L0PARM1) const options(*nopass);
iPARM2 like(dsLOGDTA.L0PARM2) const options(*nopass);
iPARM3 like(dsLOGDTA.L0PARM3) const options(*nopass);
end-pi;
dcl-proc LogOnlyParms;
dcl-pi *n;
iPARM1 like(dsLOGDTA.L0PARM1) const options(*nopass);
iPARM2 like(dsLOGDTA.L0PARM2) const options(*nopass);
iPARM3 like(dsLOGDTA.L0PARM3) const options(*nopass);
end-pi;
I'd like these three functions all to call LogData. Each of them
defaults the values that are not passed in. I know I can pass "special
values" for type and code, but for various reasons I'd really like to be
able to call without those parameters. This seemed like an easy thing
to do, but I ran into difficulties. Clearly I can't just call this way
from LogCode:
LogData( C_DEFAULT_TYPE: iCODE: iPARM1: iPARM2: iPARM3);
I'll get an MCH pointer exception if I don't pass all three variables.
I tried being a little sneakier and did this:
LogCommon( C_DEFAULT_TYPE: iCODE: %parms:
%addr(iPARM1): %addr(iPARM2): %addr(iPARM3));
I wrote a new routine LogCommon that instead of the parameters accepts
pointers to the parameters along with (for belt and suspenders security)
passing the number of parameters. I only then used the actual number of
parameters that were passed, and ignore the others. Initial testing
looked good with the address of unpassed parameters being sent as *NULL.
Unfortunately, it turns out you can't get the address of a parameter
defined this way:
iPARM1 like(dsLOGDTA.L0PARM1) const options(*nopass);
You can only get the address of this:
iPARM1 like(dsLOGDTA.L0PARM1) options(*nopass);
Specifying /const/ angers the compiler and it won't let you take the
address of the parameter.
So now I'm stuck. My guess is I'll end up with a common routine with no
optional parameters and code like this in every front end:
if %parms = 0;
LogCommon( C_DEFAULT_TYPE: C_DEFAULT_CODE: ' ': ' ': ' ');
elseif %parms = 1;
LogCommon( C_DEFAULT_TYPE: C_DEFAULT_CODE: iParm1: ' ': ' ');
elseif %parms = 20;
LogCommon( C_DEFAULT_TYPE: C_DEFAULT_CODE: iParm1: iParm2: ' ');
else;
LogCommon( C_DEFAULT_TYPE: C_DEFAULT_CODE: iParm1: iParm2: iParm3);
endif;
Has anybody else dealt with this? It's a common pattern in Java, and
I'd like to be as clean about it as possible as I start to build some
library functions.
--
This is the RPG programming on IBM i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://archive.midrange.com/rpg400-l.
Please contact support@xxxxxxxxxxxx for any subscription related
questions.
Help support midrange.com by shopping at amazon.com with our affiliate
link: https://amazon.midrange.com
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.