1. Feedback on enhancement request

Hi,

I propose that a function can "return" multiple distinct values.

eg.

function fred(...)
   integer return_code
   sequence result

   ...

   return {ret}, {result}
end function


integer a
sequence b

a , b = fred(...)

I find I often want to return two or more distinct values back from 
function.  I find it messy and difficult to re-read code after some
months that is like: 

seq = fred()
if seq[1] = ... then
   ...do soemthing witg seq[2] ...

Would this also remove the need for pass by reference?

As an example 

------
function swap(atom a, atom b)
   return {b}, {a}
end function

variable1, variable2 = swap(variable1, variable2)
-------

Anyone have any thoughts?


Ray Smith
http://www.geocities.com/ray_223

new topic     » topic index » view message » categorize

2. Re: Feedback on enhancement request

That is common problem also to me.
To do it like this:

    fred_result = fred()
    meaningful_name = fred_result [1]
    if meaningful_name = ... then

is better than like this:

    seq = fred()
    if seq [1] = ... then

Selecting the right names for your variables in very important for readable
code. Maybe the most important part in writing readable code and also a
difficult one sometimes.

But I find your source code of your libraries very good and clear.

----- Original Message -----
From: "Ray Smith" <smithr at ix.net.au>
To: "EUforum" <EUforum at topica.com>
Sent: Saturday, February 16, 2002 4:29 AM
Subject: Feedback on enhancement request


>
> Hi,
>
> I propose that a function can "return" multiple distinct values.
>
> eg.
>
> function fred(...)
>    integer return_code
>    sequence result
>
>    ...
>
>    return {ret}, {result}
> end function
>
>
> integer a
> sequence b
>
> a , b = fred(...)
>
> I find I often want to return two or more distinct values back from
> function.  I find it messy and difficult to re-read code after some
> months that is like:
>
> seq = fred()
> if seq[1] = ... then
>    ...do soemthing witg seq[2] ...
>
> Would this also remove the need for pass by reference?
>
> As an example
>
> ------
> function swap(atom a, atom b)
>    return {b}, {a}
> end function
>
> variable1, variable2 = swap(variable1, variable2)
> -------
>
> Anyone have any thoughts?
>
>
> Ray Smith
> http://www.geocities.com/ray_223
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

3. Re: Feedback on enhancement request

On Sat, 16 Feb 2002 03:29:22 +0000, you wrote:

>I find I often want to return two or more distinct values back from 
>function.

It's the multiple assign which is hard. If you could write

{a,b}=swap(b,a) or massign({a,b},swap(b,a))

then return doesn't need to change. But multiple assign is horrible,
like the sudden English "or" separating the Euphoria code above.
Quickly, what's edge set to in:

{girth,widths,height,uplift,edge,jut,throw}={3,{7,6,5,2},89,2,{3,4,2},9,{3,4}}

OK, so you might know that fairly easily,or not, but you've just read
the line of code, so without looking, what was height just set to?

For something minimalist like swap you just really want a <=>
operator, there is no better way to make it nice&sweet&short. For
anything more complicated, though
	temp=func(a,b)  a=temp[1] b=temp[2]
is justified. I use it alot and it's not so bad if you do that rather
than have [1] & [2] litter the following code. Othertimes I enumerate,
eg
constant COUNT=1, AVERAGE=2   then
 if result[COUNT]> threshold then
	...
	x=result[AVERAGE]*result[COUNT]

Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu