Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The correct solution to this problem is to enhance Class::Multimethods to handle it correctly. ;-)

The problem boils down to the fact that the first argument passed to any Perl 5 constructor is the name of the class on which the constructor was called.

So, in effect, every constructor multimethod has to have '$' as the type specifier for its first parameter. Which leaves no way to tell them apart.

But there's no reason that a multiple dispatch system has to restrict itself to considering just the types of the arguments on which it's dispatching. An obvious extension is to allow the dispatcher to examine the values of those parameters and differentiate on that basis too.

Suppose, for example, that we were to generalize Class::Multimethods so that parameters could also be specified as a subroutine. When such a variant was considered in the dispatch process, instead of working out the inheritance distance from the argument's class to the parameter class, Class::Multimethods would pass the argument to the subroutine, and use the sub's return value as the distance (with undef indicating type incompatibility).

Then one could set up subroutine generators for just about any kind of dispatch criterion. For example:

sub Value_eq { my ($target_value) = @_; sub { $_[0] eq $target_value ? 0 : undef } } sub Negative { sub { $_[0] < 0 ? 0 : undef } }

Then we would be able to code class constructors as multimethods, like so:

package Base; multimethod new => (Value_eq('Base')) => sub {...} multimethod new => (Value_eq('Base'), Negative) => sub {...} multimethod new => (Value_eq('Base'), '#') => sub {...} package Der; use base 'Base'; multimethod new => (Value_eq('Der')) => sub {...} multimethod new => (Value_eq('Der'), '$') => sub {...} # etc.

Of course, we could simplify that even further by providing a subroutine generator that specifically compared argument values against the name of the current class:

sub This::Class { my $target_value = caller; sub { $_[0] eq $target_value ? 0 : undef } }

With which we could code multiply dispatched constructors like so:

package Base; multimethod new => (This::Class) => sub {...} multimethod new => (This::Class, Negative) => sub {...} multimethod new => (This::Class, '#') => sub {...} package Der; use base 'Base'; multimethod new => (This::Class) => sub {...} multimethod new => (This::Class, '$') => sub {...} # etc.

Now, if only I had the tuits to actually make Class::Multimethods work that way! :-(

Ah least I can put it at the top of the module's ToDo list.


In reply to Re: Class::Multimethods namespace collusion by TheDamian
in thread Class::Multimethods namespace collusion by bakunin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (9)
    As of 2017-01-16 12:58 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (150 votes). Check out past polls.