Re: Rationale for $!

2016-01-28 Thread Moritz Lenz
Hi,

On 01/28/2016 04:06 PM, Todd C. Olson wrote:
> Is there a way to make the exception be thrown eagerly, at the devision 
> statement rather than waiting until use, at the say statement?

Yes, 'use fatal;'

Cheers,
Moritz


Re: Rationale for $!

2016-01-28 Thread Brandon Allbery
On Wed, Jan 27, 2016 at 11:00 AM, Felipe Gasper 
wrote:

> Unrelated, but, does open() not throw on failures anyway? (Noodling with
> the perl6 REPL just now seems inconclusive.)


There have been issues with failures in sink context not throwing, IIRC? So
how you were noodling can matter.


-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Rationale for $!

2016-01-28 Thread Brandon Allbery
On Wed, Jan 27, 2016 at 11:06 AM, Felipe Gasper 
wrote:

> On 27 Jan 2016 11:03 AM, Brandon Allbery wrote:
>>
>> On Wed, Jan 27, 2016 at 11:00 AM, Felipe Gasper > > wrote:
>>
>> Unrelated, but, does open() not throw on failures anyway? (Noodling
>> with the perl6 REPL just now seems inconclusive.)
>>
>> There have been issues with failures in sink context not throwing, IIRC?
>> So how you were noodling can matter.
>>
>
> felipe@Macintosh-3 00:45:46 /
> > perl6
> > open("/dfgsgsdfgd", :r);
> Failed to open file /dfgsgsdfgd: no such file or directory
> > my $f = open("/dfgsgsdfgd", :r);
> Failed to open file /dfgsgsdfgd: no such file or directory
> > print $f;
> (HANDLED) Failed to open file /dfgsgsdfgd: no such file or directory
>   in any  at
> /Users/felipe/.rakudobrew/moar-2015.12/install/share/perl6/runtime/CORE.setting.moarvm
> line 1
>   in block  at  line 1
>

Looks to me like both of them threw? and $f is set to the Failure object
instead of a filehandle, which has an annotation saying that it was indeed
handled (thrown).

Perl 6 has two ways to indicate failure: you can throw immediately, or you
can return a Failure object which is a "lazy exception". The latter allows
a program to introspect the return value to recognize and handle a Failure
without actually throwing it (faster and often cleaner than catching it),
or if something just tries to use the value without checking then it throws
at that point. It remembers the context in which it was created, so the
traceback should indicate that it happened in the open call.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Rationale for $!

2016-01-28 Thread Todd C. Olson
Slight change in focus of question ...

> On We 2016-01-27, at 09:15, Felipe Gasper  wrote:
> ---
> use v6;
> 
> my $x = 10;
> my $y = 0;
> 
> my $z = $x / $y;
> 
> my $exception;
> {
>{
>say $z;
>CATCH {
>default {
>$exception = $_;
>}
>}
>}
> }
> 
> if ($exception) {
>say "There was an exception: $exception ($!)";
> }
> 
> say "still running";
> ---


Is there a way to make the exception be thrown eagerly, at the devision 
statement rather than waiting until use, at the say statement?

Regards,
Todd Olson

Re: Rationale for $!

2016-01-27 Thread Peter Pentchev
On Wed, Jan 27, 2016 at 10:32:46AM -0500, Felipe Gasper wrote:
[snip]
> But, what is the point of $! at all? The exception is given to the CATCH
> block as $_. If I want access to it outside CATCH, isn’t the expected
> workflow to save CATCH{$_} to a variable, the way my example does it?

Well, it makes it possible to write this concise thing:

  try my $f = open ...;
  die "Could not open $fname: $!" if $!;

G'luck,
Peter

-- 
Peter Pentchev  r...@ringlet.net r...@freebsd.org p...@storpool.com
PGP key:http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13


signature.asc
Description: PGP signature


Re: Rationale for $!

2016-01-27 Thread Moritz Lenz

On 01/27/2016 04:32 PM, Felipe Gasper wrote:

On 27 Jan 2016 10:15 AM, Moritz Lenz wrote:


On 01/27/2016 03:15 PM, Felipe Gasper wrote:

So, what *is* the scoping of $!?


Scoped to a routine, iirc (sub, method, regex)


Interesting. JavaScript programmers that I’ve known bemoan that their
language uses function scoping rather than block scoping.

That also seems incongruent with the built-in block scoping for try/CATCH.

Has Perl 6 embraced function scoping as a major paradigm, then?


I wouldn't say "major paradigma", but it's not the only construct that 
uses routine scoping. return() and fail() come to mind, which are also 
routine-scoped.



But, what is the point of $! at all?


Convenience. It makes it easy to write commonly-used constructs much faster.

My mostly unscientific approach to gather usage of try vs. CATCH in the 
ecosystem:
moritz@hack:~/p6/perl6-all-modules$ git grep --word CATCH | wc -l 


461
moritz@hack:~/p6/perl6-all-modules$ git grep --word try | wc -l
864

CATCH is also rather clunky by comparison (requires an explicit block, 
whereas 'try' can be used as a statement prefix; requires a "when" or 
"default" blocks).



$! is also not mentioned here: http://perl6intro.com/#_exception_handling


Feel free to fix that by submitting a pull request :-)

Cheers,
Moritz


Re: Rationale for $!

2016-01-27 Thread Felipe Gasper

On 27 Jan 2016 10:56 AM, Moritz Lenz wrote:


But, what is the point of $! at all?


Convenience. It makes it easy to write commonly-used constructs much
faster.

My mostly unscientific approach to gather usage of try vs. CATCH in the
ecosystem:
moritz@hack:~/p6/perl6-all-modules$ git grep --word CATCH | wc -l
461
moritz@hack:~/p6/perl6-all-modules$ git grep --word try | wc -l
864

CATCH is also rather clunky by comparison (requires an explicit block,
whereas 'try' can be used as a statement prefix; requires a "when" or
"default" blocks).


Yeah … obviously I wasn’t part of the discussions, but ISTM a more 
straightforward syntax could have been found. 


-F


Re: Rationale for $!

2016-01-27 Thread Felipe Gasper

On 27 Jan 2016 11:03 AM, Brandon Allbery wrote:


On Wed, Jan 27, 2016 at 11:00 AM, Felipe Gasper > wrote:

Unrelated, but, does open() not throw on failures anyway? (Noodling
with the perl6 REPL just now seems inconclusive.)


There have been issues with failures in sink context not throwing, IIRC?
So how you were noodling can matter.



felipe@Macintosh-3 00:45:46 /
> perl6
> open("/dfgsgsdfgd", :r);
Failed to open file /dfgsgsdfgd: no such file or directory
> my $f = open("/dfgsgsdfgd", :r);
Failed to open file /dfgsgsdfgd: no such file or directory
> print $f;
(HANDLED) Failed to open file /dfgsgsdfgd: no such file or directory
  in any  at 
/Users/felipe/.rakudobrew/moar-2015.12/install/share/perl6/runtime/CORE.setting.moarvm 
line 1

  in block  at  line 1
>

-F



Re: Rationale for $!

2016-01-27 Thread Felipe Gasper

On 27 Jan 2016 10:44 AM, Peter Pentchev wrote:

On Wed, Jan 27, 2016 at 10:32:46AM -0500, Felipe Gasper wrote:
[snip]

But, what is the point of $! at all? The exception is given to the CATCH
block as $_. If I want access to it outside CATCH, isn’t the expected
workflow to save CATCH{$_} to a variable, the way my example does it?


Well, it makes it possible to write this concise thing:

   try my $f = open ...;
   die "Could not open $fname: $!" if $!;



Well, the problem there is that we shouldn’t have to check for failures; 
failures should check whether we handled them!


Could it not be:

try my $f = open(...) or die …

??

Personally, I don’t mind eval {} in Perl 5; I just $@ were 
dynamically-scoped the way $1, $2, etc. are. That would solve most of 
the issues with it.


Unrelated, but, does open() not throw on failures anyway? (Noodling with 
the perl6 REPL just now seems inconclusive.)


-F


Re: Rationale for $!

2016-01-27 Thread Felipe Gasper

On 27 Jan 2016 10:15 AM, Moritz Lenz wrote:


On 01/27/2016 03:15 PM, Felipe Gasper wrote:

So, what *is* the scoping of $!?


Scoped to a routine, iirc (sub, method, regex)


Interesting. JavaScript programmers that I’ve known bemoan that their 
language uses function scoping rather than block scoping.


That also seems incongruent with the built-in block scoping for try/CATCH.

Has Perl 6 embraced function scoping as a major paradigm, then?

But, what is the point of $! at all? The exception is given to the CATCH 
block as $_. If I want access to it outside CATCH, isn’t the expected 
workflow to save CATCH{$_} to a variable, the way my example does it?


$! is also not mentioned here: http://perl6intro.com/#_exception_handling

-FG


Re: Rationale for $!

2016-01-27 Thread yary
On Wed, Jan 27, 2016 at 11:00 AM, Felipe Gasper 
wrote:

> Could it not be:
>
> try my $f = open(...) or die …
>

Don't need a "try" there to make it work. An exception object/failure is
false, so "my $f = open(...) or die" will assign the exception to $f, which
is false, causing the "die" to execute.

-y


Re: Rationale for $!

2016-01-27 Thread Moritz Lenz

Hi,

On 01/27/2016 07:17 AM, Felipe Gasper wrote:

Hello,

 What is the purpose of having $! in Perl 6?

 The global variables in Perl 5 are a constant headache, prompting
us to need to local()ize variables like $@, $!, and $? to avoid
unforeseen consequences like RT #127386 and those documented in
Try::Tiny’s POD.

 Perl 6 seems to give us both the “right” and the “wrong” solution
to accessing exception variables: we get $_ within a CATCH block
(right), but we also get that global $!--which, to me, seems
pathologically wrong.


But it's not global! None of $_, $/, $! are global.

Cheers,
Moritz


Re: Rationale for $!

2016-01-27 Thread Felipe Gasper

On 27 Jan 2016 7:15 AM, Moritz Lenz wrote:

On 01/27/2016 07:17 AM, Felipe Gasper wrote:

Hello,

 What is the purpose of having $! in Perl 6?

 The global variables in Perl 5 are a constant headache, prompting
us to need to local()ize variables like $@, $!, and $? to avoid
unforeseen consequences like RT #127386 and those documented in
Try::Tiny’s POD.

 Perl 6 seems to give us both the “right” and the “wrong” solution
to accessing exception variables: we get $_ within a CATCH block
(right), but we also get that global $!--which, to me, seems
pathologically wrong.


But it's not global! None of $_, $/, $! are global.


I modified the example I posted before to test that:

---
use v6;

my $x = 10;
my $y = 0;

my $z = $x / $y;

my $exception;
{
{
say $z;
CATCH {
default {
$exception = $_;
}
}
}
}

if ($exception) {
say "There was an exception: $exception ($!)";
}

say "still running";
---

If you run this, $! is populated even when we’ve gone back a block level 
from where the exception was thrown.


So, what *is* the scoping of $!?

-FG


Re: Rationale for $!

2016-01-27 Thread Moritz Lenz


On 01/27/2016 03:15 PM, Felipe Gasper wrote:

So, what *is* the scoping of $!?


Scoped to a routine, iirc (sub, method, regex)