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

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

Thread Previous | Thread Next
Ricardo Signes
August 25, 2012 06:22
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
* Ricardo Signes <> [2012-08-25T08:07:40]
> >         my $reading = Interval->new($raw_data, plus_minus=>$uncertainty);
> > 
> >         if ($reading ~~ 0) {...}
> > 
> >         # i.e. want Interval's overloading, not a numeric match against
> >         #      $reading object's address
> No, we want the same as C< $reading == 0 >, which can be done with current
> overloading.  (In fact, which is done in my own Number::Tolerant for just this
> means.)

Another counterexample, harkening somewhat back to the one of the much earlier
examples from Damian.

  for (@input) {
    when (0) { warn "can't divide by zero" }

    say $total / $_;

Seems safe, right?  Well, if (similar to David Golden's "Error" example) an
element of @input wants to "force" stringwise matching against the zero and its
dual-var-ish string representation, even though there is 0+ overloading, then
this will explode.  (In David's example, it wanted to force numification on
itself despite being matched against a string-function-using test; I have only
reversed the scenario.)

Again:  I really think the test-writer (that is, the programmer chosing the
matching (not matched) operand) needs to be in control of the behavior of the


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