http://www.perlmonks.org?node_id=412561


in reply to Re: Use method/function signatures with Perl
in thread Use method/function signatures with Perl

Several things. Let's compare. Here's some code from the CMM docs:

multimethod find => (Container, Query) => sub { $_[0]->findquery($_[1]) }; multimethod find => (Container, Sample) => sub { $_[0]->findlike($_[1]) }; multimethod find => (Index, Word) => sub { $_[0]->lookup_word($_[1]) };

And how would that compare with Sub::Signatures?

sub find(Container $container, Query $query) } $container->findquery($query); }; sub find(Container $container, Sample $sample) } $container->findlike($sample); }; sub find(Index $index, Word $word) { $index->lookup_word($word) }; }

Mine is easier to use and a bit more flexible if you merely want to dispatch based on the number of arguments. It's also easier to understand because it looks like just about every other language out there as opposed to the CMM syntax. This means existing code is more easily ported to/from this solution. Also, my code (IMHO) is simpler than Damian's, though he does his without the source filter. I guess it all boils down to the fact that I've never felt comfortable with the interface to CMM.

Also, my code is more than multi-method dispatch. Just having function signatures is a nice bonus and you can even use them with anonymous subs (in 0.1 and without the MMD.)

In short, there's nothing wrong with Damian's solution. The major difference in code lies in my trying to solve a problem of a somewhat different scope. At this point, the only significant advantage to his code is the fact that it's been around a lot longer is likely to be more robust as a result. My code quite possibly has serious limitations that I haven't accounted for. That's why I'm looking forward to bug reports :)

Cheers,
Ovid

New address of my CGI Course.