RCR 225: Named Return Arguments
submitted by chemdog on Sun Feb 29 2004 03:20:57 AM -0800
Status: withdrawn
Abstract
Allow a method to name its return arguments in the method definition. Allow a method call site to retrieve the named arguments it wants.
Problem
When a method returns multiple values, usually in an array, it is in general impossible to know at call time if my call site has retrieved the values in the right order. As an example h = Net::HTTP.new(website, 80) response, data = h.get(webpage, nil) Did I put the response and data variables in the right order? ( Yes, this time.) Functionality for ensuring that the right results get into the correct variable is needed.
Proposal
I propose to change method definitions to include Named Return Arguments. And to change method call sites to paste arguments the right way. Using the previous example the method definition would change to def [response, data] get(page, header=nil) the old user code still works, but additionally the call site can include argument glue. The examples for this are h = Net::HTTP.new(website, 80) [:response response, :data data] = h.get(webpage, nil) [:data data2] = h.get(webpage2, nil) [:response response3] = h.get(webpage3, nil) [:data data4, :response response4] = h.get(webpage4, nil) In the second and third examples, the response2 and data3 are discarded at the call site. Every method name could be prefixed by an return argument name array. At method invocation time, each named return element is set to nil. Whenever an assignment occurs within a method to a return argument name, the assignment is bound to the returned value. A return statement by itself, assembles the return array using the current values of the named return argument variables. A return statement with arguments, has its arguments expanded to match the format of the return call. A short array is padded with nil entries. A long array is truncated to fit within the values. The last statement, if it returns a value, is used as if it were a return statment passing that value, and if it doesn't return a value, is treated as an empty return. The named argument list can use a star to indicate the remaining arguments. As in "def [stuff, stuff2, *rest] variable_length_method". Additional entries in a long return are packed into the last argument. Call-sites can use named glue to bind a named return argument to a call-site array position. Additionally, the call-site can bind a star argument in any position; however, it receives an array of the remainder of the arguments. For example, [:stuff stuff, :rest rest, :stuff2 stuff2] = variable_length_method # receives "elem1","elem3-elemN","elem2" This change shouldn't break any old code, however the old interpreter would be unable to handle the new method def's and glue bindings.
Analysis
There is no way currently to enforce/ensure that the right return values are being received in multiple return value situations. It is a symmetric problem to keyed call-arguments(using key-values to write call-arguments(hash arguments)).
Implementation
nil
Add comments here
Vote for this RCR
Add comments here
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog