Re: [DOS] Can we emulate "pass by reference" in Eu?
- Posted by "Christian Cuvier" <Christian.CUVIER at agriculture.gouv.fr> Oct 19, 2004
- 532 views
> > posted by: Alex Caracatsanis <CaracatsanisA at ramsayhealth.com.au> > > Dear List > > For my general knowledge, rather than with a specific program in mind, I'd > like to ask how to pass a non-global variable (two scenario's: an atom; a > sequence) as an argument to: > 1 a procedure that changes the variable (eg increments it) > 2 a function that changes the variable (eg increments it) but returns some > other, different, variable > > A "minimal" example to achieve the above, would be enough. > > Thank you > > Alex Caracatsanis Yes:
sequence sharedvars sharedvars=repeat(0,27) --whatever max limit to the number of shared variables --extending it dynamically is trickier, see below procedure proc1(integer passedbyref,object othervars) object whatever --some code sharedvars[passedbyref]=whatever --... end procedure --... sharedvars[slot]=myarg proc1(slot,optionallySomethingElse) --sharedvars[slot] holds the modified value
Replace "procedure" by "function" as needed. After all, that's how the network interrupt #5C works. You have to keep track of slot alloction and return types yourself. Now some code in order to forget about any max size of sharedvars:
sequence sharedvars sharedvars={0,0,0} integer usedslots usedslots=0 function allotslot(object x) if usedslots=length(sharedvars) then sharedvars&=0 end if usedslots+=1 sharedvars[usedslots]=x return usedslots end function procedure endgame() usedslots=0 end procedure --... procedure proc1(integer passedbyref,object othervars) object whatever --some code sharedvars[passedbyref]=whatever --... end procedure integer slot slot=allotslot(myarg) proc1(slot,optionallySomethingElse) --sharedvars[slot] holds the modified value --... endgame() --current tempvars no longer valid, clean up
That's clumsier, but you no longer take care of slots; just remember which holds what. And you can typecheck sharedvars[slot] at any time and take any appropriate action. CChris