Re: Pass by Reference

new topic     » goto parent     » topic index » view thread      » older message » newer message
jaygade said...

The question is whether you are willing to perform your task in an idiomatic way or try to force the language to doing things your way.

I understand that Euphoria don't have pass by reference. I think I've got a pretty good grasp on the "idiomatic" way of doing it in Euphoria.

I also understand that pass by reference is somehow "bad" or "unsafe", but it's not clear why this is considered to be the case.

This is especially confusing, since I use it all the time in various other programming languages.

Consider if Euphoria did allow passing by reference, using a '*' character in the parameter list of the declaration:

procedure swap(object *a, object *b) 
   object tmp = a 
   a = b 
   b = a 
end procedure 

It's clear in the declaration that the routine swap expects to be able to modify a and b. So you don't actually have to read the code to see what it's going to do.

Having implemented pass by reference in Py, I should note an obvious caveat: you can only pass assignable values. For example:

-- not legal 
swap( 1, 2 ) 

The reason for the limitation was because behind the scenes, Py was basically doing the same sort of thing I was complaining about not wanting to write:

procedure swap(object a, object b) 
   object tmp = a 
   a = b 
   b = a 
   return {a, b} 
end procedure 
 
object_result = swap( 1, 2 ) 
1 = result[1] -- not legal 
2 = result[2] -- also not legal 
object_ = 0 -- deref 

Pure Euphoria, with the parser handling all the nasty details.

Of course, if the parser is smart enough, it can simply just not emit code for non-assignable expressions.

Here's another example:

function foo(integer *a, integer b) 
   a = 23 
   return 0    
end function 
 
integer a = 11 
integer b = foo(a, 11) 

This would be transformed to something more like:

function foo(integer a, integer b) 
   foo = 23 
   return {0, foo} 
end function 
 
integer a = 11 
object_result = foo(a, 11) 
a = result[2] 
integer b = result[1] 
result_ = 0 -- deref 

So it's certainly something that Euphoria is capable of, and it's not even a "dangerous" as a goto, since it doesn't break any existing rules.

- David

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu