On Monday, March 31, 2003, at 11:21 AM, Smylers wrote:
Michael Lazzaro writes:
Forgive me; a very minor style & efficiency question... what would the
canonical way to do this be, given what we know of Perl6?

     # the hapless, inefficient version:
     return &result_of_some_big_long_calculation(...args...)
         if &result_of_some_big_long_calculation(...args...);

The obvious answers are this:

     my bool $x = &result_of_some_big_long_calculation(...args...);
     return $x if $x;

That does something different, in that it has coerced the result into a
C<bool>[*0]. So after the first line C<$x> can only be 0 or 1[*1]. And
given that one of those states is false, the code becomes equivalent to:
<snip>
However if you permit the function to return more than two different
values (of which more than one are true) then it becomes a more
interesting question.

My apologies, you are correct -- $x should _not_ be typed. It's the more interesting question I'm asking.


On Monday, March 31, 2003, at 12:30 PM, Paul wrote:
I started to suggest this myself, then realized that you might not want
it to return at all if the value is false.

Yes, exactly:


  sub foo(...args...) {
    # We first attempt to get our return value the easy way.
    # If successful (the resulting value is defined and true),
    # just return that value.

    my $x = &baz(...args...);
    return $x if $x;

    # Still here?  OK, then... we've got a lot more work
    # to do before we can return a reasonable value

    ...
  }

I'm looking for a Perl6 way to say that oft-repeated, oft-chained two-line snippet up there without declaring the temporary variable. Using C<given> or C<when>, maybe?

MikeL



Reply via email to