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

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
Ben Bullock
June 17, 2021 00:37
Re: RFC 0004 - defer {} syntax
Message ID:
On Thu, 17 Jun 2021 at 07:15, Paul "LeoNerd" Evans <>

> The name "defer" comes from a collection of other languages.
> Near-identical syntax is provided by Swift, Zig, Jai, Nim and Odin. Go
> does define a `defer` keyword that operates on a single statement,
> though its version defers until the end of the containing function, not
> just a single lexical block.

"defer" in Go is used to catch ("recover") exceptions ("panic") which
covers "very bad" things like memory errors (segmentation fault, etc.) so I
think it has to be at the "end of the function" since it is basically being
called as you go back down the stack of functions when recovering from a
"disaster" in a called routine. For example if you write an http server in
Go and the function you pass to make the web page crashes, the http server
doesn't stop functioning but just prints a message and continues.

That requirement doesn't seem to apply here.

> I did consider this difference, but ended
> up deciding that a purely-lexical scoped nature is cleaner and more
> "Perlish", overriding the concerns that it differs from Go.

I can imagine cases where you might want to clean up after each go around
of a loop. But I find it quite hard to imagine how it would be useful to
have a function which runs at the end of an "if" statement block regardless
of what else happens.

Would it be useful to make the "defer" statement within an "if" block refer
to the enclosing function/loop/other block rather than the "if" block, in
the same way that a "next" statement within the if block refers to the
enclosing loop?

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About