Re: Pass by Reference
- Posted by dcuny Jan 12, 2015
- 2307 views
Yep, I'm tracking now. Not convinced of the value of this yet, however...
Sorry. I had some examples, but they got edited out.
Here's my logic: Euphoria still doesn't support functions passing back multiple values. A function can still only pass back a single value.
What's been added is a syntax that makes it much easier to unpack a sequence. And this is a good thing.
If I write:
function foo(integer a, integer b, integer c) return 12 end function ? foo()
and try to run it, this is the result I get:
<0236>:: 'foo' needs 3 arguments ? foo()The compiler is able to clearly identify an mismatch between the two.
Now consider the following:
function foo(sequence this, integer update_value) integer prior_value = this[1] this[1] = update_value return {this, prior_value} end function sequence this = {50} this = foo(this, 11) ? this
The intent of the function is to replace the prior value in the first position, and return the old value. Since it's emulating pass by reference, it also returns the sequence. However, there's a logic error, so this ends up storing the both the updated sequence and the prior value.
The problem is that the parser doesn't understand the intent. Only I (the developer) knows if the function intended to return a sequence, or a list of values.
This goes back to my argument that just because something can be done minimally, that doesn't mean it should be done that way.
Consider instead this (hypothetical) example:
function foo(sequence this, integer update_value) integer prior_value = this[1] this[1] = update_value return this, prior_value end function sequence this = {50} integer prior_value this = foo(this, 11) ? this
Unlike the prior declaration of foo(), it's clear by looking at the function that the routine is intended to return multiple values, and not a single sequence. The hypothetical parser could return an error:
<xxxx>:: 'foo' returns 2 values this = foo(this, 11)That's why I keep hammering on the word intent. Otherwise, the functions aren't really returning multiple values. We've just provided a means for Euphoria to emulate the behavior. But emulation isn't the same as actually doing it, and the job of "making it work" falls on the developer.
Another example: Euphoria doesn't need type checking. Sure, it helps the interpreter generate faster code. But it also stops you from passing bad datatypes to routines and variables. It's so common, we take it for granted.
There's also a symmetry to the use of the comma, in the same way that:
{a, b} = {b, a}
there's a strong mapping between the use of commas in the return and the assignment:
function foo() return 1, 2, 3 end function a, b, c = foo()
By adding this syntax to Euphoria, you're no longer emulating the behavior, and you get the full benefit: the parser can catch logical errors because it sees what was written didn't follow the intent.
It also (happily) happens to follow the syntax of other language (Lua, Python) which have functions that return multiple values.
- David