On Mar 23, 2007, at 8:52 PM, Guyren Howe wrote:
> On Mar 23, 2007, at 8:48 PM, Guyren Howe wrote:
>
>> Why should returning a single value be easy but multiple values be a
>> pain in the proverbial? I don't see anything special about returning
>> one value.
>
> Is there no-one else on this mailing list who uses any of the many
> languages that have multiple return values who will jump in here and
> confirm that, in fact, this turns out not to be a huge mis-feature
> in those languages, that it works fine, that it does not *actually*
> cause your hair to turn out or require you to sacrifice your first-
> born?
No one suggested it did so that's just being dramatic
Ruby doesn't return multiple values. A return only ever returns one
and only one thing, a tuple, BUT Ruby happens to unpack it for you.
The effect is that it appears to return multiple items.
def m1
return 1, 2, 3
end
def m2
return [1, 2, 3]
end
m1 and m2 both return one tuple
m1 # -> [1, 2, 3]
m2 # -> [1, 2, 3]
From what I know Python and Perl do the same automatic unpacking of
returned tuples
In Python these two functions both return one tuple
a, b, c = 0, 0, 0
def getabc():
a = 1
b = 2
c = 3
return a,b,c # this defines a tuple on the fly
def gettuple():
a,b,c = 1,2,3
return (a,b,c) # this explicitly creates one tuple to return
a,b,c = getabc()
a,b,c = gettuple()
One defines it on the fly with "return a,b,c" the other explicitly
returns a single tuple (a,b,c)
Applescript even does this with a return of multiple values. It
allows you to name the items in the tuple when you return them so you
can reference them in the tuple by name OR by position (item 1
of ...) etc
on testFunc()
return {a:1, b:2}
end testFunc
set thevalue to testFunc()
log a of thevalue
log b of thevalue
(test this and show the event log)
I've used several of these and really don't see why in RB a simple
class with properties is not suitable to you
At worst for now use a dictionary and unpack it (which is what Ruby,
Python, etc are automating for you)
At least a dictionary you can key value code so you can return the
tuple in ANY order and it will work
In RB this code
function getabc() as dictionary
dim d as new dictionary
d.value("c") = 3
d.value("b") = 2
d.value("a") = 1
return d
can be constructed and unpacked correctly in ANY order unlike the
multiple return values version in Python
You can write
dim d as dictionary = getabc()
if d.hasKey("c") then c = d.value("c")
if d.hasKey("a") then a = d.value("a")
if d.hasKey("b") then b = d.value("b")
and it will work
Wrapping a dictionary into a "tuple class" with operator lookups you
could skip all the "hasKey" coding could be done once and reuse it
all over the place
Then you could just write, quite simply,
dim d as tuple = getabc()
c = d.c
a = d.a
b = d.b
There are, admittedly some shortcomings.
A variant, like the value in a dictionary cannot hold an array. That
I'd love to see amended.
However, a dictionary with an integer key can be traversed much like
an array in the mean time.
As for why there is "usually" one result to a function I suspect that
has a lot to do with heritage and old compiler designs.
Often in old processors you had a limited number of registers for
things like accumulators, subroutine jumps, etc and you could NOT
return values on the stack like you can push arguments on the stack.
So, conventions led to "returns results are in such and such a
register" because you had to put them somewhere that had not been
wiped out when you returned from a function.
That was almost always a register.
RB probably does much the same hence the one return result in a
particular register.
All that said, make the request and see where it lands.
It may get some priority, it may not.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>