Re: pass by reference
- Posted by Kat <gertie at PELL.NET> Feb 12, 2002
- 598 views
On 12 Feb 2002, at 13:26, Irv Mullins wrote: > > On Tuesday 12 February 2002 12:07 pm, Bernie Ryan wrote: > > > > > > I keep reading that you can't use pass by reference > > in Euphoria. That is not true. You can use pass by > > reference on any variable or atom or any fixed > > length structure. The only thing you can't pass by > > reference is a sequence because it is dynamic and > > there is no way to control the errors that would > > be caused by a user. The way you pass by reference > > is to use allocates, peeks and pokes. > > er...no, that's pass-by-value. > If I call foo(integer x) as follows: > > x = 3 > foo(x) > ? x => 3 > > No matter what happens inside the mysterious foo, x is still what it was > before calling the function. > Even if foo sets x to 99, it's only 99 while inside the foo routine. > So foo is working with a copy of x, not the real x. > > Pass-by_reference passes the actual variable, not a copy, so that > foo(var integer x) > x = 99 > end > > x = 3 > foo(x) > ? x => 99 > > The variable IS changed within the routine, and remains changed afterward. This has been useful in the past, mostly to jump thru hoops in strong typing and other such diversions. I don't know how useful it would be for me in Eu now, but i am not against it, it could attract users as much as the goto could. Likewise the users of ref-passing could write interesting "features" into their programs. However, if the suggestion below is introduced, another whole world could open to Ai and gamers and maybe thread interaction options............ I still have a suggestion (you just *knew* i would, now didn't you?), if we get into passing pointers to the actual var (whether global or local to the calling function), make the pointers a user-accessable table (pronounce "sequence") containing all the variables in use at the time. The table should contain the name of the var, and the pointers one would pass for each var. If this doesn't exist now, it would prolly slow Eu a bit, so being able to switch on/off the updating of the table would be a plus. I could then see someone using this info in a really really really useful IDE, where one could modify the vars while stepping thru the program, set flags/breaks on when the vars change, trace who changed them in the IDE, etc. etc... in addition to the program being able to know what it knows vs what it doesn't know, which is a problem in Ai. Also, the table would be useful for a function like: function monitor_myvar(object var) if equal(var.name,"albatross") then if (var.val > "landing") then scream("crashlanding!!") end if end if if equal(var.name,"abort") then -- code to try a recovery end if -- a loop follows, naturally: if equal(var.name,breakvar.*.name) then if (var.val > breakvar.*.setpoint) then -- alert the IDE to begin stepping end if end if end function on_varchange(monitor_myvar(^albatross)) Kat