develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
Ben Bullock
Date:
June 20, 2021 10:51
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
CAN5Y6m9Tqo+bHCFr=tQ2OtB3OaOU8Q1OPkfSwMMGJjzhqy9uJg@mail.gmail.com
On Thu, 17 Jun 2021 at 20:24, Arne Johannessen <arne-9544@thaw.de> wrote:

> Ben Bullock <benkasminbullock@gmail.com> wrote:
> >
> > I can see an advantage from being able to run defers conditionally.
>
> consider:
>
>     defer if EXPR { ... }
> vs:
>     defer { ... if EXPR }
>
> The second form is already allowed by the syntax proposed in this RFC, so
> the benefit of also allowing the first form seems to be little.
>

I have not expressed any opinion about that.

use feature 'say';
use Syntax::Keyword::Defer;
while (1) {
  if (1) {
    defer {
      say "A";
    }
    say "B";
  }
  say "C";
  last;
}

prints

B
A
C

My suggestion is that it might be preferable to run the defer outside the
scope of the if block, so the above would print

B
C
A

I suggested this because I can't see how running a deferred block of code
at the end of an "if" block would be useful in practice, since, for example
there is no "next" or "last" which makes it possible to leave the if block
early, whereas being able to conditionally stack defers probably would be
useful.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About