Skip Menu |

This queue is for tickets about the Syntax-Keyword-Try CPAN distribution.

Report information
The Basics
Id: 121267
Status: new
Priority: 0/
Queue: Syntax-Keyword-Try

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: TODO: value semantics
There's two parts to this feature - the syntax, and the implementation. Syntax-wise, I wonder if it would be nicer to keep the 'try' syntax purely for control flow, similar to things like 'if' and 'foreach', and add different looking syntax for value-returning semantics, so that the reader is less surprised by nonobvious behaviour. Perhaps the 'do' keyword could be used here as is used in other bits of perl syntax, e.g. my $value = do try { ... } catch { "default" }; my $value = try do { ... } catch { ... }; Implementation-wise, there are difficulties in propagating the runtime gimme context correctly into the body of the try block. Solving this one might require creating a new runtime op that can adjust the context dynamically. In particular, static analysis isn't good enough in such situations as 'do try' blocks being returned in tail position of a function sub { ...; return do try { foo() } } -- Paul Evans
Version 0.09_001 contains an experimental attempt at value semantics via the 'try do { ... }' syntax. It's a little buggy, in that local'isation of $@ fails for perls before 5.24. It *almost* makes do { try { ... } } happen to have value-returning semantics, except that to do that would break @ localisation and finally blocks for perls before 5.24 as well, so for now I haven't enabled that. If a solution to localisation and finally blocks on perls before 5.24 can be found, then maybe the do { try ... } notation will be considered "good enough" not to need this current hack of try do { ... } -- Paul Evans
This is now available in 0.10. As a further thought on the ongoing experiment, a better spelling might be $value = checked { BLOCK } ... taking some inspiration from the Go/Rust/Swift set of languages considering syntax looking like handle { code for exception handler } result := check function_call(); This notation also decouples the `catch` away from an individual `try` block and out into general (preceding) lexical scope above. That's a bit more of a radical change from here so I'm not going to consider it yet, but it is something that -might- work in future. Or perhaps a different module? Thoughts continue... -- Paul Evans