develooper Front page | perl.perl5.porters | Postings from August 2012

Re: fixing smartmatch just hard enough (and when, too)

Thread Previous | Thread Next
Damian Conway
August 22, 2012 16:48
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
Jesse Luehrs suggested:

> So maybe what we really want is to move that case down below the "Any ->
> Code" case.

I don't think so. If overloading is tested first, then the overloading
itself can implement
a fallback to the standard Any ~~ CodeRef case.

If Any ~~ CodeRef is tested first, then new classes have no way to
intercept and
control that behaviour sensibly for their objects.

In fact, I would argue that the overloading case should come *first*
in the table,
even before the Any ~~ undef case, so that classes overloading smartmatch can
intercept and handle that case too.

> If $b is a coderef, I'd expect it to be called with the junction,
> rather than calling the ~~ overload on the left hand side.

So what if I want *correct* junctive behaviour in that case: namely that
each element of the junction is distributed over a separate call to the sub
and the results are rejunctified.

You're proposing that:

    any(9,10,11) ~~ \&is_prime

be equivalent to:


but what if I want it to mean what it's supposed to mean:

   any(is_prime(9), is_prime(10), is_prime(11))

If overload testing comes first, I can implement the Junction class's
overloading to do that. If CodeRef testing comes first, I'm screwed.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About