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

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
Ben Bullock
Date:
June 21, 2021 01:36
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
CAN5Y6m8Brx734PuEmzY4O7CK8g+kkAZE4He=sKgiOy9KquW0uw@mail.gmail.com
On Sun, 20 Jun 2021 at 21:09, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>
wrote:

> On Sun, 20 Jun 2021 19:51:10 +0900
> Ben Bullock <benkasminbullock@gmail.com> wrote:
>
> > 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,
>
> The clean "mental model" of how it operates.
>
>   "defer {} runs its code when we hit the next nested }"
>
> As opposed to
>
>   "defer {} runs its code when we hit the next nested } that wasn't an
>   if{} or elsif{} or unless{}, or maybe we'd consider adding do {} to
>   that list since you can't next out of them and what about match/case
>   blocks and ...."
>

Thank you for responding.

"next" and "last" within if statement blocks go to the surrounding loop
block rather than the if block. Similarly "return" in an "if" block returns
from the function rather than jumping out of the if block. I don't see
anyone complaining that that return or next or last do not provide a clean
mental model.

What I find confusing is that, as far as I know, there is not an obvious
way to exit the if{} without actually getting to the end of it, so

   if (something) {
      defer {
        cleanup;
      }
      do something;
      now do the deferred cleanup.
    }

doesn't offer an advantage over

    if (something) {
      do something;
      cleanup;
    }

I went back and checked, and I've actually said this in all of the messages
I've sent on this thread, and so far nobody has pointed out the mistake in
my understanding. Is there some way of sneaking out of if{} without
arriving at the end of the code?

If "defer" is there to catch a "return" statement in the if block, then why
does "defer" need to be associated with the end of the if block? It could
just as well be associated with the end of the function for that case.
Similarly if it's meant to catch a "next" statement for an enclosing loop.
In both cases the scope which you are guarding is bigger than the if block,
it is either the function scope for the "return", or the loop scope for the
"next".

So I can't see what benefit there is in being able to defer to the end of
an if block scope. Did I miss something obvious?

It might be easier to understand for some people if set up this way, but
for me it was quite confusing why the defer would need to be run at the end
of if {}.

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