Newbie on multiple return values 
Author Message
 Newbie on multiple return values

Am I right in assuming that the following is the proper way to declare a
method that returns two numbers?

define method returns-two-numbers(just-some-arg :: <number>)
      => (one-result :: <number>, another-result :: <number>);
//  actual code, ending with
      values(foo, bar)
end method;

The DIRM is not clear on the point.

--

I've begun to suspect that large portions of the Universe -- possibly including history itself -- do not properly reward fair play, and I tell you I'm pretty worked up about it.



Sat, 16 Aug 1997 05:57:02 GMT  
 Newbie on multiple return values

Quote:

>Am I right in assuming that the following is the proper way to declare a
>method that returns two numbers?

>define method returns-two-numbers(just-some-arg :: <number>)
>      => (one-result :: <number>, another-result :: <number>);
>//  actual code, ending with
>      values(foo, bar)
>end method;

>The DIRM is not clear on the point.

Yes, this is the correct syntax (although you are missing a ; at the end of
the function).

You can specify *NO* return values as well, with:

define generic returns-nothing () => ();
        // does nothing either.
end;

// author:          Patrick C. Beard
// organization:    Computing Research Group



Sun, 17 Aug 1997 05:09:33 GMT  
 Newbie on multiple return values

Quote:

> define method returns-two-numbers(just-some-arg :: <number>)
>       => (one-result :: <number>, another-result :: <number>);
> //  actual code, ending with
>       values(foo, bar)
> end method;

> The DIRM is not clear on the point.

Actually, the DIRM does say how return (or what it calls result value)
values are handled, but it isn't a few pages after you first see the
information on things like "define method".

What you have up there is good. Remember, you don't actually have to have
the result values list there if you choose not to have it. It will help
you document your code and let the compiler perform extra optimization as
well as exception handling, but it isn't needed.

  ** Ken **

****      Kenneth Knight       Innovation Associates  ****
****                Software Engineer                 ****
****   voice: (313)-995-9338    fax: (313)-995-9338   ****



Sun, 17 Aug 1997 05:38:47 GMT  
 Newbie on multiple return values

Quote:
>Am I right in assuming that the following is the proper way to declare a
>method that returns two numbers?
>define method returns-two-numbers(just-some-arg :: <number>)
>      => (one-result :: <number>, another-result :: <number>);
>//  actual code, ending with
>      values(foo, bar)
>end method;

As someone who usually uses either standard ML or C/C++ I am
a little puzzled by this convention.

Would it not be simpler to have a tuple type
and pattern matching ? Or is the requisite
pattern matching difficult to add to Dylan ?

Is this use of values iherited from Lisp or what ?

--

Simon Kinahan            "Only in our dreams are we truly free,




Tue, 26 Aug 1997 06:30:08 GMT  
 Newbie on multiple return values

Quote:


(Fritz Anderson) writes:

> >Am I right in assuming that the following is the proper way to declare a
> >method that returns two numbers?

> >define method returns-two-numbers(just-some-arg :: <number>)
> >      => (one-result :: <number>, another-result :: <number>);
> >//  actual code, ending with
> >      values(foo, bar)
> >end method;

> As someone who usually uses either standard ML or C/C++ I am
> a little puzzled by this convention.

> Would it not be simpler to have a tuple type
> and pattern matching ? Or is the requisite
> pattern matching difficult to add to Dylan ?

Not really sure what you're asking here, but let me see take a stab at an
answer.

Unlike C/C++ where you can return a single value via a return statement
you can return multiple values in Dylan. I'm using the term value loosely
here to mean any type of object. In the example above the values could
only be an instance of <number> (remember that includes <number>'s
subclasses). We could easily change it to be of type <list> or give no
special information at all and simply return the values we want. Again, in
the above fragment, you could return just one value and the second value
would be returned as #f. If we added "#rest <object>" to the return values
you would have a method that required you to return 2 <number>s and then
you could return anything else you wanted (e.g. values(3, 4, "#(1,2,3,4),
#"abc" - is legal).

What this gives you is the ability to clearly handle side-effects which
can be a headache in C. And with the type specialization you havve control
over the dynamism is the method you create.

  ** Ken **

****     Kenneth Knight         Innovation Associates     ****
****                    Software Engineer                 ****
****  voice: (313)-995-9338      fax: (313)-995-9338      ****



Wed, 27 Aug 1997 12:40:10 GMT  
 Newbie on multiple return values

Quote:
>>Am I right in assuming that the following is the proper way to declare a
>>method that returns two numbers?

>>define method returns-two-numbers(just-some-arg :: <number>)
>>      => (one-result :: <number>, another-result :: <number>);
>>//  actual code, ending with
>>      values(foo, bar)
>>end method;

>As someone who usually uses either standard ML or C/C++ I am
>a little puzzled by this convention.

>Would it not be simpler to have a tuple type
>and pattern matching ? Or is the requisite
>pattern matching difficult to add to Dylan ?

>Is this use of values iherited from Lisp or what ?

When using the tuple approach, you can lose (or make much more difficult)
an optimization that makes real construction of tuple unnecessary
if it
will be split into components just after return. It will make some run
time
(and GC time) difference when tuples are allocated from heap, and, in
some
cases, even when tuples are stack-allocated (by putting unnecessary
restrictions
on the exact positioning of variables in the stack frame).

Oleg



Fri, 29 Aug 1997 22:47:42 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. return multiple values from an awk function?

2. ???Eiffel idiom for multiple return values???

3. Tuples, iterators, and multiple return values in Eiffel?

4. Wait ms Timer Multiple returning inaccurate timer value

5. multiple values in a return from function

6. Multiple return values

7. multiple return values

8. Multiple return values

9. multiple return values for functions

10. Multiple return values

11. 1st-class method closures (was Re: Multiple return values)

12. C-interface and multiple return values

 

 
Powered by phpBB® Forum Software