[tpm] Answers to some questions for tonight's talk

zoffix at zoffix.com zoffix at zoffix.com
Wed Mar 30 20:22:45 PDT 2016


Thanks again for having me.

Now that I got a Perl 6 compiler at my hands to play with, I can  
answer a couple of questions that were asked:


Better messages for where { ... } subsets. The code in the `where` can  
be anything you want, so you can `warn` or `fail` inside the check to  
get a better error message. Once caveat: the argument given to  
`callframe` might be different depending on where you're performing  
the check. Try adjusting it:

     subset Foo of Int where {
         $_ > 10_000
             or fail "You need a number more than 10,000 on "
                 ~ "line {(callframe 4).line}, but you passed $_";

     my Foo $x = 1000;

     # OUTPUT:
     #  You need a number more than 10,000 on line 7, but you passed 1000
     #  in block <unit> at test.p6 line 2


As far as testing whether something fits the subset, you can use
this trick with trying to assign to a variable and catching the  
exception. It feels a bit like a hack, but I'm unsure if there's a  
better way:

     subset Foo of Int where {
         $_ > 10_000
             or fail "You need a number more than 10,000 on "
                 ~ "line {(callframe 4).line}, but you passed $_";

     my $value = 42;
     try { my Foo $x = $value; CATCH { fail "It's no good" }; };
     say "It's fine";

     # OUTPUT:
     #  It's no good
     #    in block  at test.p6 line 8
     #    in block <unit> at test.p6 line 8


"Can you have an infinite Set?"

No, it tries to actually create one. Makes sense, since a set cares  
about the elements. Sure, it's possible to special-case some forms of  
sequences to figure out whether an element is part of the sequence or  
not, but it's probably not worth it. In a more general case, you are  
faced with the Halting Problem. Speaking of which, here is a gotcha  
with the sequence operator and the upper limit:

     my @seq = 0, 2 ... * == 1001;

Here, I'm using the sequence operator to create a sequence of even  
numbers, and I'm limiting the upper bound by when it'd be equal to  
1001. But it won't ever be equal to that. To human brain, it might  
seem obvious that once you're over 1001, you should stop here, but to  
a computer it's a Halting Problem and it'll keep trying to find the  
end point (so it'll never complete here).


Places to learn Perl 6: along with http://perl6intro.com/ that I  
mentioned during the talk, there's also Learn X in Y Minues Perl 6  
page, which I personally found very useful when just starting out with  
Perl 6: https://learnxinyminutes.com/docs/perl6/

I'm also including the link to the Ecosystem:  
http://modules.perl6.org/  you should have `panda` program installed,  
and you can install modules from the Ecosystem by typing `panda  
install Foo::Bar`

These are all that I can remember being asked.

Hope it helps.


More information about the toronto-pm mailing list