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

Re: Thoughts in the direction of a core try/catch syntax

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
June 27, 2020 12:39
Subject:
Re: Thoughts in the direction of a core try/catch syntax
Message ID:
20200627133927.30c596a6@shy.leonerd.org.uk
On Sat, 27 Jun 2020 13:48:31 +0200
Paul Johnson <paul@pjcj.net> wrote:

> Summary: I'd love to get a very simple try/catch syntax in core as an
> experimental feature.
> 
> 
> I'm mostly happy with Syntax::Keyword::Try, especially with the
> enhancements described below made possible by being core.  I don't
> really like "finally" as implemented and would be happy to leave it
> out of an initial core implementation.

I agree; see my other post just now about a LEAVE phaser.

> I don't need any kind of type system/matching initially and would
> suggest that anything in that area should be more general and
> shouldn't be considered for an initial implementation here.

Please lets not confuse "implementation" with "design". I would still
like to *design* what shape a type-matching part of the syntax would
look like, even if just approximately.

I don't want to start implementing a simple "catch all" version if it
turns out that design makes it hard to squeeze in the type stuff later
on. I want to know where the thing is heading, so we don't start off
going in the wrong direction.

> I don't
> really care what variable the exception ends up in in the catch
> block.  Being fast would be nice.
> 
> I think a simple syntax such as that described below is sufficiently
> clean that it should always work, whatever other bells and whistles
> may subsequently be added.

Yes I don't think the initial bits below were particularly contentious.
It's all the other bits about type matching/dispatch that make it
complicated.


Lets make a really concrete example: Here is some existing Perl code
that you can write right now, using plain ol' eval{}.

  unless(
   eval { somefunc(); 1 }
  ) {
    my $e = $@;
    if   ($e isa X::SomeException) { say "Red" }
    elsif($e =~ m/^Cannot do a thing /) { say "Blue" }
    else { rethrow }
  }

How would you see rewriting that in a hypothetical core 'try'? I
suggest for example:

  use feature 'try';

  try {
    somefunc();
  }
  catch my $e (isa X::SomeException) { say "Red" }
  catch my $e (=~ m/^Cannot do a thing /) { say "Blue" }

Which is non-ideal due to multiple `my $e` and those dangling binary
operators `isa` and `=~` just starting the parens there. They look a
bit ugly IMHO.

Can we find better?

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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