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 ****