4 ms·
> One of my pet perl hates, the inability to write "sub foo($a, $b, $c) {" is fixed. I think you're in for a good surprize there, you can even do subset Even
by patrickas 16y ago
> One of my pet perl hates, the inability to write "sub foo($a, $b, $c) {" is fixed.
I think you're in for a good surprize there, you can even do
subset Even of Int where { $_ % 2 == 0 };
sub foo(Even $a) {say $a;}
foo(2); # prints 2
foo(3); # Constraint type check failed for parameter '$a'
- someone_here 16y agoI think I'm falling for perl all over again <3
- elblanco 16y agooh God that looks absolutely delicious.
- draegtun 16y agoYep very scrummy! And its also been available (for methods) in Perl5 for a while as well: http://news.ycombinator.com/item?id=1040614 http://news.ycombinator.com/item?id=1040614 Here is the perl6 example using MooseX::Declare (http://search.cpan.org/dist/MooseX-Declare/ http://search.cpan.org/dist/MooseX-Declare/) in perl5: use 5.012; use MooseX::Declare; class Foo { use MooseX::Types -declare => [ qw(Even) ]; use MooseX::Types::Moose qw(Int); BEGIN { subtype Even, as Int, where { $_ % 2 == 0 }; } method bar ( Even $a ) { say $a } } my $foo = Foo->new; $foo->bar( 2 ); # => prints 2 $foo->bar( 3 ); # => throws error
- elblanco 16y agoThis is a cool capability. But I have to admit, it's lacking in the syntactic sugar that made the previous example look so appealing. I just simply read the code out loud in English and immediately knew what it meant. This is a bit more of a tongue twister. This notion of defining subsets of symbols like this could make code correctness for many cases a trivial thing.
- winthrowe 16y agoWell, can you reasonably expect much else? It's a backported feature from perl6, constrained by the existing syntax of perl5. Of course it won't have the syntatic sugar. One of the other cool capabilities of perl6 is the rules system, expanding on regexs so you can work with full on grammars. The perl6 grammar, written in perl6: http://svn.pugscode.org/pugs/src/perl6/STD.pm6 http://svn.pugscode.org/pugs/src/perl6/STD.pm6
- elblanco 16y agoNo, I'm simply stating that I really like what they're doing with Perl 6 so far. I'm excited by the new grammar system immensely. It's clear that doing it in an older syntax is clearly less elegant. However, it's very impressive though that the back ports of Perl 6 features to Perl 5 are proceeding relatively nicely though. The work on Perl 6 has definitely been a boon to 5.
- draegtun 16y agoThe BEGIN { subtype ... } code is just pure perl/Moose. So its possible for the MooseX::Declare developers to add some extra sugar there.
- phaylon 16y agoIt goes away as soon as you put the MooseX::Types declaration into its own package. Which one does pretty often, since reusing types and coercions produces more maintainable code in general. The title of that library isn't "Organise your Moose types in libraries" without reason :)
- kilowatt 16y agoOne of my favorite examples of cool Perl 6 code right now is Perl 6 in Perl 6: http://github.com/masak/yapsi/blob/master/lib/Yapsi.pm http://github.com/masak/yapsi/blob/master/lib/Yapsi.pm
- draegtun 16y agoOne perl6 project that looks very interesting is http://github.com/krunen/xml/ http://github.com/krunen/xml/ XML parser defined using perl6 grammar rules: http://github.com/krunen/xml/blob/master/lib/XML/Grammar/Document.pm http://github.com/krunen/xml/blob/master/lib/XML/Grammar/Doc...