Re: Pass by Reference
- Posted by petelomax Dec 18, 2014
- 3330 views
One solution would be to pass a sequence back from the function, which is really clunky:
function do_filter( sequence theFilter, atom theInput ) atom theOutput = theFilter[2] * theFilter[1] * theInput theFilter[2] = theFilter[1] theFilter[1] = theInput return {theFilter, theOutput} end function sequence theResult = do_filter( theFilter, theInput ) theFilter = theResult[1] atom theOutput = theResult[2]
how about
{theFilter,theOutput} = do_filter(theFilter,theInput)
But it raises the question: why do I have to jump through these hoops at all? Why can't OpenEuphoria let me pass values by reference like just about any other language?
If a language knows, without a doubt, that a passed item cannot be modified by the called routine, then many optimisation possibilities open up for it.
Phix actually supports PBR, but ONLY for local values:
global sequence gtable global atom gthing {gtable,gthing} = filter(gtable,gthing) -- NOT PBR procedure p() sequence ltable atom lthing {ltable,lthing} = filter(ltable,lthing) -- pbr end procedure
Technically, it isn't pbr, but reference counts of 1, and works by making the locals unassigned over the call, which is perfectly reasonable since they are going to get overwritten as soon as it returns. When I say "NOT PBR", I am in fact referring to internal behaviour and copy-on-write semantics that impact performance, rather than functionality. If you wanted pbr optimisation effects on gtable,gthing you would need something a bit messy:
ltable = gtable lthing = gthing gtable = {} gthing = 0 {ltable,lthing} = filter(ltable,lthing) {gtable,gthing} = {ltable,lthing}
which is only needed, of course, if you insist on having those nasty globals in the first place, and indeed cared about a few copy-on-writes.
IMNSHO, Euphoria is one of the most unsafe languages I've ever coded in.
It crashes on errors... by design.
The core datatype - the sequence - is inherently unsafe. It's trivial to get an index out of bounds error. (I speak from experience!)
I'd argue that Euphoria is unsafe by design.
And I would totally agree with you, but for completely different reasons. I would in fact shout it from the rooftops as one of the BEST IDEAS EVER. Compared to C++ just quietly going wrong, anyway.
Pete
PS: disclaimer: the above expresses how it is designed to work, rather than any assurance it is available and working like that right now.