Skip Menu |

This queue is for tickets about the MooseX-Types CPAN distribution.

Report information
The Basics
Id: 73667
Status: open
Priority: 0/
Queue: MooseX-Types

People
Owner: Nobody in particular
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Overload smart match
package MooseX::Types::TypeDecorator; use overload '~~' => sub { my ($self, $val) = @_; $self->__type_constraint->check($val); }; This allows: if ($value ~~ PositiveInt) { print "It's a positive int\n"; } And even: given ($value) { when (PositiveInt) { print "It's a positive int\n"; } when (NegativeInt) { print "It's a negative int\n"; } }
As further info, this suggestion is inspired by Perl 6 that allows you to smart match against bareword type names. e.g. this Perl 5: if (ref $foo eq 'ARRAY') { say "foo is an array" } becomes this in Perl 6: if $foo ~~ Array { say "foo is an array" }
On Mon Jan 02 04:35:45 2012, TOBYINK wrote: Show quoted text
> As further info, this suggestion is inspired by Perl 6 that allows you > to smart match against bareword type names. > > e.g. this Perl 5: > > if (ref $foo eq 'ARRAY') { > say "foo is an array" > } > > becomes this in Perl 6: > > if $foo ~~ Array { > say "foo is an array" > } >
I think it'd make sense to hold off and see how smart match changes in future Perls. The current implementation may be deprecated and replaced with something simpler.
On 2012-01-23T19:40:26Z, DROLSKY wrote: Show quoted text
> I think it'd make sense to hold off and see how smart match changes
in Show quoted text
> future Perls. The current implementation may be deprecated and
replaced Show quoted text
> with something simpler.
As I understand it, the plan is to possibly change the implementation in favour of DOY's smartmatch::engine stuff which allows pluggable smartmatch engines, with at least two engines bundled with Perl: a backward compatible one, plus a simplified one used by default for "use 5.16" and above. Having looked at the code for both of these engines, the overloading I suggest would work under both of them. http://cpansearch.perl.org/src/DOY/smartmatch-engine-core-0.02-TRIAL/ lib/smartmatch/engine/core.pm https://github.com/doy/smartmatch-engine-rjbs/blob/master/lib/ smartmatch/engine/rjbs.pm
There are a few likely futures for ~~. These are presented in no particular order: 1. nothing changes, and we beg people not to use it 2. it is formally deprecated, to be removed at some point 3. it is given a second, simpler, better set of behaviors In (1) and (3), the meaning is going to remain, abstractly, $value ~~ $test In (2) it will, also, until it is fired into the sun, discussed thenceforth only in hushed tones. The same things go for given/when. I think the only thing making $value~~$moosetype a questionable idea is that it might encourage people to start using the badly-behaving things related to it: given (with its lexical topic) and the worser test types. This might be sufficiently addressed by a sentence of warning. Apart from that, Moose types are exactly the kind of thing that should overload ~~.
Looks like nuffin already got this working in a branch years ago; it's just not been merged into master... http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo/MooseX- Types.git;a=shortlog;h=refs/heads/topic/smartmatch