Hi, Little Walker wrote: > I've been looking around to see if there's been any discussion of > introducing functional programming-style pattern matching for method/ > function dispatch. Could someone point me to any such discussions?
It's done multi dispatch in Perl 6, and you can find an introduction in a book chapter which we're writing these days: http://github.com/perl6/book/blob/master/src/multi-dispatch.pod > I've seen that perl6 is taking on a lot of ideas from the functional > world (lazy evaluation for instance). With multi-method dispatch on > the agenda pattern matching seems inevitable but I haven't been able > to find information about this or any related discussions. > > This is a contrived example of what I'm referring to: > > sub traverse([Leaf $a]) { > # do something > } > > sub traverse([Tree $left, Tree $right]) { > traverse($left); > traverse($right); > } > > my $t = Tree(...); > traverse($t); You got it almost right. The simples approach is to use "normal" matching: multi traverse(Leaf $a) { # do something } multi traverse(Tree $t) { traverse($t.left); traverse($t.right); } The latter uses a bit of additional code for unpacking the $t parameter; you can do that in a signature too (which is not yet covered in the book chapter; you can find some documentation here: <http://perlcabal.org/syn/S06.html#Unpacking_tree_node_parameters>) multi traverse (Tree $t ( $left, $right) ) { traverse($left); traverse($right); } Since you don't actually access $t anywhere, there's no reason to give it a name, so you can write this actually as multi traverse (Tree $ ( $left, $right) ) { traverse($left); traverse($right); } Hope that helps, Moritz -- Moritz Lenz http://perlgeek.de/ | http://perl-6.de/ | http://sudokugarden.de/