Submitted by chemdog (Sun Feb 29 08:02:57 UTC 2004)
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.
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.
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .