PxPlus User Forum
Main Board => Discussions => Language => Topic started by: Frank Dreßler on December 12, 2019, 08:35:20 AM
-
I was trying to accept a variable number of arguments to a function so that all of them appear as elements of an array in the function.
x = new("VARARG_TEST")
x'test(123)
! Expected result inside test(): params[1] = 123
x'test(11, 22, 33, 44, 55)
! Expected result inside test(): params[1] = 11, params[2] = 22, params[3] = 33, params[4] = 44, params[5] = 55
drop object x
My current approach can be seen in the following code. "enter_vararg" enters the arguments into an array "params{all}"
def class "VARARG_TEST"
function test(*) test
end def
test:
gosub enter_vararg ! Make arguments available inside params{all}
print params{all}
return
enter_vararg:
dim params[*]
skip$ = ""
while 1
execute "enter " + skip$ + " x", err=*break
params[*] = x
skip$ += "*,"
wend
return
This looks a bit clunky because of skip$ and execute. Is there a better way to solve this problem?
-
"enter_vararg" is like doing the following:
enter_vararg:
dim params[*]
local x
enter x, err=*return; params[*] = x
enter *, x, err=*return; params[*] = x
enter *, *, x, err=*return; params[*] = x
enter *, *, *, x, err=*return; params[*] = x
enter *, *, *, *, x, err=*return; params[*] = x
! ...
return
-
TCB(20) tells you how many arguments there are, but it doesn't check for non-numbers.
STK(PROPERTIES) tells you how many arguments there are and the type of each argument (https://manual.pvxplus.com/PXPLUS/functions/stk.htm).
Using that you can create something like this:
00010 !
00020 DEF CLASS "vararg_test"
00030 FUNCTION test(*)TEST
00040 END DEF
00050 !
00060 !
00070 TEST:
00080 LET properties$=STK(PROPERTIES)
00090 IF properties$="" THEN EXIT 23
00100 IF POS("Nn"^properties$)>0 THEN EXIT 26
00110 !
00120 LET properties_count=LEN(properties$)
00130 LET iolist$="IOLIST "
00140 FOR i=1 TO properties_count
00150 LET iolist$+="param"+STR(i)+","
00160 NEXT i
00170 LET iolist$=CPL(STP(iolist$,"R",","))
00180 !
00190 ENTER IOL=iolist$
00200 DIM params[1:properties_count]
00210 READ DATA FROM REC(iolist$) TO params{ALL}
00220 !
00230 FOR i INDEX params{ALL}
00240 PRINT i,params[i]
00250 NEXT i
00260 RETURN 1
00270 !
->x=new("vararg_test")
->x'test(123)
1 123
->x'test(11,22,33,44,55)
1 11
2 22
3 33
4 44
5 55
-
Has the STK(Properties) function been updated to reflect read-only arrays?
-
No -- we actually hit a bit of syntax issue with regards to this as a string array returns "A" and a numeric array returns "a".
Generally when we had a Read only element we returned the lower case (e.e. a Numeric element is "N" but if read only then its "n").
This posed a problem when it came to arrays and we didn't want to break anything.
-
I noticed that, hence the question.
How about "X/x", or "P/p" for String/numeric 'Protected' arrays?