Re: RE: pass by reference
- Posted by Derek Parnell <ddparnell at bigpond.com> Feb 19, 2002
- 493 views
20/02/2002 2:21:46 PM, Kat <gertie at PELL.NET> wrote: > >Wouldn't the PassByReference camp be satisfied with a table of the vars? >And maybe a table of the functions, like routine_id()s for everything? I have a > >feeling this already exists in Eu, but we don't have access to it. Then, with >exception handling, you could check whatever vars you wanted, and do >whatever you need to do with them. > I can see where you are going here. Something like variable_id() that would return a reference to the variable. This idea seems nice but it also creates other side-effects. For example, what exactly would it return? Currently routine_id() returns an integer. If variable_id() returned an integer, how would we distinguish between an integer and a data reference in the routine it was passed to. procedure ProcA( integer x) . . . end procedure -- This might cause bugs. ProcA(1) -- This might not be right either? ProcA( variable_id(x) ) One solution is to create a new datatype, say a 'reference'. Then we could do: procedure ProcA( reference x) . . . end procedure -- This would fail due to datatype mismatch. ProcA(1) -- This would be fine. ProcA( variable_id(x) ) But a new datatype would create new overheads in the interpreter. Everywhere it currently validates datatypes, it would have to add another check. We would probably also need a reference() function similar to sequence() etc... However, a reference datatype would still enable Eu automatic garbage collection and memory allocation to work. It would still not allow direct access to a variable's RAM, but that could be argued as a good thing. We could do things like: sequence A, B integer C reference x,y C = 1 x = variable_id(C) x = 0 -- Change C to zero. A = "foo" B = "bar" x = variable_id(A) y = variable_id(B) B = x -- Copy A to B x = "moo" -- Change A to "moo" x[3] = 'p' -- Change A to "mop" function swap (reference a, reference b) object temp if ( (atom(a) and atom(b)) or (sequence(a) and sequence(b)) or (reference(a) and reference(b)) ) then temp = a a = b b = temp return 1 else return 0 end if end function C = swap (x, y) -- now A is "bar" and B is "mop" C = swap (variable_id(A), variable_id(B)) -- now B is "bar" and A is "mop" C = swap (variable_id(x), variable_id(y)) -- now x refers to B, and y refers to A. ------------- These are not show-stoppers, just things to weigh up in the scheme of things. Sure would make OO a lot easier to implement. Of course, if you didn't want a routine to change you're data, if passed by reference, you should be able to enclosed it in parenthesis to have Eu create a temporary copy of the data before passing it. sequence X ProcA ( (variable_id(x)) ) then if ProcA tried to modify the variable, it would only modify the temporary copy and not the original. -------- cheers, Derek